detail pro num implement handle uil ret eas
安卓闹钟可以配合广播来实现(不推荐),系统资源浪费,安卓系统在 5.0 以后的定时 任务貌似触发时间不准了,因为了为了省电。
- //获取系统闹钟
- AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
- Intent intent = new Intent(ReportDetailsActivity.this, ReportDetailsActivity.MyReceiver.class);
- pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),0, intent,0);
- //开启定时任务
- alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() +1000,5 *1000, pendingIntent);
记得在 manifeast 文件配置该广播
- publicstaticclassMyReceiverextendsBroadcastReceiver {@OverridepublicvoidonReceive(Context context, Intent intent) {if (bo >0) {if (bo >240) {//刷票
- handler.sendEmptyMessage(3);//弹窗警告 刷票
- }else {
- handler.sendEmptyMessage(2);
- }
- }
- }
- }
在 OnDestroy() 中取消闹钟
- @OverrideprotectedvoidonDestroy() {
- alarmManager.cancel(pendingIntent);
- }
睡 5s 中去定时操作任务。
- classMyRunnableimplementsRunnable{
- @Overridepublic void run() {while (isLoop){try {if (bo >0) {if (bo >240) {//刷票
- handler.sendEmptyMessage(3);//弹窗警告 刷票
- }else {
- handler.sendEmptyMessage(2);
- }
- }
- Thread.sleep(5000);
- }catch (Exception e){
- e.printStackTrace();
- }
- }
- }
- }
在 onCreate() 方法中开启:
- loopThread=newThread(new MyRunnable());
- loopThread.start();
在页面销毁时终止掉该 Thread
- isLoop=false;
- loopThread.interrupt();
开启 timer
- Timer timer=new Timer();
- timer.schedule(new TimerTask() {
- @Override
- public void run() {
- //TODO...
- }
- },new Date(),5000);
终止 timer
- timer.cancel();
以上三种定时任务除了第一种不要随便使用外,推荐使用第三种和第二种。
Java + 安卓 定时任务
来源: http://www.bubuko.com/infodetail-2039428.html