android中的定时任务。由AlarmManager对象进行管理
1.定时唤醒广播接受者
直接贴代码,在代码中说明
AlarmController.java
public class AlarmController extends Activity{private Toast mToast;private static final String TAG = "app";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.alarm_controller);Button button = (Button)findViewById(R.id.one_shot);button.setOnClickListener(oneShotListener);button = (Button)findViewById(R.id.start_repeating);button.setOnClickListener(startRepeatingListener);button = (Button)findViewById(R.id.stop_repeating);button.setOnClickListener(stopRepeatingListener);}private OnClickListener oneShotListener = new OnClickListener() {@Overridepublic void onClick(View v) {//只进行一次定时,不重复执行AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);Calendar calendar = Calendar.getInstance();calendar.setTimeInMillis(System.currentTimeMillis());calendar.add(Calendar.SECOND, 30);//设置30秒后执行/* * 根据PendingIntent.getBroadcast * 指明定时唤醒广播接收者 * */PendingIntent pending = PendingIntent.getBroadcast(AlarmController.this, 0, new Intent(AlarmController.this,OneShotAlarm.class), 0);am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pending);  //表示在指定时间后执行if(mToast!=null){mToast.cancel();}mToast.makeText(AlarmController.this, "one_shot_scheduled", Toast.LENGTH_LONG).show();}};private OnClickListener startRepeatingListener = new OnClickListener() {@Overridepublic void onClick(View v) {//重复执行AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);PendingIntent pend = PendingIntent.getBroadcast(AlarmController.this, 0, new Intent(AlarmController.this,RepeatingAlarm.class), 0);long triggerAtTime = SystemClock.elapsedRealtime();triggerAtTime +=15*1000;//表示第一次执行15秒后am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,   triggerAtTime, 15*1000, pend);//表示过15秒重复唤醒广播接受者if(mToast!=null){mToast.cancel();}mToast.makeText(AlarmController.this, "repeating_scheduled", Toast.LENGTH_LONG).show();}};private OnClickListener stopRepeatingListener = new OnClickListener() {@Overridepublic void onClick(View v) {//取消alarmAlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);PendingIntent pend = PendingIntent.getBroadcast(AlarmController.this, 0, new Intent(AlarmController.this,RepeatingAlarm.class), 0);am.cancel(pend);if(mToast!=null){mToast.cancel();}mToast.makeText(AlarmController.this, "stop_repeating_scheduled", Toast.LENGTH_LONG).show();}};}


然后可以定期执行OneShotAlarm和RepeatingAlarm中的onRecevier中的内容

2.1.定时创建service
关键代码:
//注意此方法PendingIntent.getService()PendingIntent pend = PendingIntent.getService(this, 0, new Intent(this,AlarmService_Service.class), 0);AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);long firstTime = SystemClock.elapsedRealtime();am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, 15*1000, pend);


完整例子:


更多相关文章

  1. [置顶] [Android] AsyncTask使用实例---加载网络图片
  2. android 使用gdb调试的方式
  3. 浅析android下如何通过jni监控wifi网络连接、dhcpcd执行和power
  4. Android上传文件到Web服务器,PHP接收文件(二)
  5. FregServer进程,发送BC_TRANSACTION,唤醒ServiceManager进程,返回BR
  6. Android(安卓)ADB(Android(安卓)Debug Bridge)简单使用
  7. Android(安卓)SDK Emulator: Compile CyanogenMod
  8. Android运行底层linux外部命令的实现
  9. Android(安卓)单元测试 Robolectric

随机推荐

  1. Android GirdView
  2. Android 4编程入门经典
  3. [Android实例] 【Kris专题】android 换
  4. Android中常用的Intent启动服务
  5. Android layout xml总结(1)
  6. Android(安卓)滚动事件 OnScrollListener
  7. Android自动化测试之MonkeyRunner录制和
  8. android google map key申请
  9. Android视音频录制实现步骤(Android学习随
  10. android系统移植学习笔记一