Android使用AlarmManager實現(xiàn)定時推送功能

Android定時進行消息通知到通知欄

要實現(xiàn)本地定時通知最主要的是要用到系統(tǒng)的鬧鐘管理器AlarmManager來定時發(fā)送一條廣播,使用NotificationManager類來寫顯示在通知欄的信息。

AlarmManager:定時喚醒系統(tǒng)執(zhí)行任務(wù),即使系統(tǒng)處于深度睡眠也能喚醒。
AlarmManager的使用步驟:

  1. 獲得ALarmManager實例
  2. 定義一個PendingIntent發(fā)出廣播
  3. 調(diào)用ALarmManager方法,設(shè)置定時或重復(fù)提醒
  4. 取消提醒

發(fā)送通知步驟:

  1. 獲取狀態(tài)欄通知管理NotificationManager
  2. 配置Notification.Builder
  3. 綁定Notification,發(fā)送通知請求。

注意:要使得APP能在Android8.0中顯示通知,需要進行版本判斷,然后給builder設(shè)置Channel。

具體實現(xiàn)如下:
首先通過AlarmManager進行定時,在指定時間發(fā)送一條廣播

    //獲取到AlarmManager對象
    AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);
    long firstTime = SystemClock.elapsedRealtime();    // 開機之后到現(xiàn)在的運行時間(包括睡眠時間)
    Long secondsNextMorning =getSecondsNext(17,46);
    Intent intentMorning = new Intent(this, AlarmBroadcastReceiver.class);
    intentMorning.setAction("CLOCK_IN");
    //獲取到PendingIntent的意圖對象
    PendingIntent piMorning = PendingIntent.getBroadcast(this, 0, intentMorning, PendingIntent.FLAG_UPDATE_CURRENT);     //設(shè)置事件
    manager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + secondsNextMorning, piMorning); //提交事件,發(fā)送給 廣播接收器  

  
   //獲取當(dāng)前時間到指定時間點的時間差
private Long getSecondsNext(int hour,int minute) {
    long systemTime = System.currentTimeMillis();

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, hour);
    calendar.set(Calendar.MINUTE, minute);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    // 選擇的定時時間
    long selectTime = calendar.getTimeInMillis();
    // 如果當(dāng)前時間大于設(shè)置的時間,那么就從第二天的設(shè)定時間開始
    if(systemTime > selectTime) {
        calendar.add(Calendar.DAY_OF_YEAR, 1);
        selectTime = calendar.getTimeInMillis();
    }
    // 計算設(shè)定時間到現(xiàn)在時間的時間差
    Long seconds = selectTime-systemTime;

    return seconds.longValue();
}

新建一個廣播接收者AlarmBroadcastReceiver,注意需要在AndroidManifest.xml里面進行注冊

public class AlarmBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals("CLOCK_IN")) {
        //獲取狀態(tài)通知欄管理
        NotificationManager manager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder= new NotificationCompat.Builder(context);
        //對builder進行配置
        builder.setContentTitle("上班打卡") //設(shè)置通知欄標(biāo)題
                .setContentText("上班打卡") //設(shè)置通知欄顯示內(nèi)容
                .setPriority(NotificationCompat.PRIORITY_MAX) //設(shè)置通知優(yōu)先級
                .setDefaults(DEFAULT_ALL)
                .setSmallIcon(R.drawable.ic_launcher)
                .setAutoCancel(true); //設(shè)置這個標(biāo)志當(dāng)用戶單擊面板就可以將通知取消
        Intent mIntent=new Intent(context, MainActivity.class);  //綁定intent,點擊圖標(biāo)能夠進入某activity
        PendingIntent mPendingIntent=PendingIntent.getActivity(context, 0, mIntent,PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(mPendingIntent);
        manager.notify(0, builder.build());  //綁定Notification,發(fā)送通知請求
       }
    }

}
?著作權(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)容