AlarmManager 實現(xiàn)定時任務(wù)

因為自己項目需要一個計時任務(wù),考慮到種種原因,項目大概需要的是這樣的 只要進程不被殺死,每間隔30分鐘向后臺發(fā)送一次服務(wù),將地圖的地位點發(fā)送到后臺,考慮到如果用handler 和Thread的或者是Timer 的話考慮一個當時屏幕熄滅或者鎖屏的情況下可能會出現(xiàn)問題。所以最后的選擇方案是AlarmManager(鬧鐘服務(wù))、BroadCastReceiver、Service實現(xiàn)了一個計時器
` ```

主要實現(xiàn)思路

  • 首先啟動一個Service
  • Service onStartCommand()方法中,只要執(zhí)行完畢你需要的操作,然后寫一個鬧鐘服務(wù)多長時間發(fā)送一次
  • 在廣播的方法onReceive()在調(diào)起服務(wù)這樣就會輪詢一個計時任務(wù)(注意:Android 8.0 不再允許后臺service直接通過startService方式去啟動)也就是講后臺不能直接startService()后面上代碼會提到這是我在寫的時候遇到的一個坑
    此??來震這篇文章

    #行了,廢話說了直接上代碼

第一步開始計時的時候啟動Service

Intent intent = new Intent(this, LongRunningService.class);
 startService(intent);

第二步在ServiceonStartCommand()方法中啟動輪詢

public void run() {
              Log.d("LongRunningService", "executed at " + new Date().
                        toString());
            }
        }).start();
        AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);
        //int anHour = 10* 1000; // 10秒
        int anHour = 1000; // 1秒
        long triggerAtTime = SystemClock.elapsedRealtime() + anHour;
        //要跳轉(zhuǎn)的廣播
        Intent i = new Intent(this, AlarmReceiver.class);
        //通過PendingIntent跳轉(zhuǎn)  這里不多做解釋  
        PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
        manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, pi);

第三步在BroadcastReceiver的onReceiver()中

//只是適配8.0之前的
Intent i = new Intent(context, LongRunningService.class);
 context.startService(i);

優(yōu)化第三步

 Intent i = new Intent(context, LongRunningService.class);
        //判斷是否是8.0版本
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //這是8.0以后的版本需要這樣跳轉(zhuǎn)
            context.startForegroundService(i);
        } else {
            context.startService(i);
        }

但是這樣啟動Service以后必須在Service中執(zhí)行startForeground(-1,new NotificationCompat().build())
否則會報ANR;
適配8.0版本第二步的優(yōu)化

new Thread(new Runnable() {
            @Override
            public void run() {
              Log.d("LongRunningService", "executed at " + new Date().
                        toString());
            }
        }).start();
        AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);
        //int anHour = 10* 1000; // 10秒
        int anHour = 1000; // 10秒
        long triggerAtTime = SystemClock.elapsedRealtime() + anHour;
        Intent i = new Intent(this, AlarmReceiver.class);
        PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
        manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, pi);
        String CHANNEL_ID = "my_channel_01";
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this, CHANNEL_ID)

                        .setContentTitle("My notification")
                        .setContentText("Hello World!");

        startForeground(-1,mBuilder.build());
     //   stopForeground(true);
        stopSelf();

項目不需要適配8.0手機不需要改動
源碼下載:
https://github.com/biao941122/Alarm

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容