android定時喚醒_訊息通知
阿新 • • 發佈:2019-01-10
1.定時喚醒
在使用service進行後臺長時間工作時,cpu或網路在休眠或深度睡眠的情況下不能正常工作
通過alarm進行定時的開啟服務
程式碼片段
2.訊息通知private void startTicker() { if (mPendingIntent == null) { mPendingIntent = PendingIntent.getService(this, 0, new Intent( "com.action.ticker"), 0); } if (mAlarmManager == null) { mAlarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); } mAlarmManager.cancel(mPendingIntent); mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 2 * 60000, mPendingIntent); } private void stopTicker() { if (mPendingIntent == null) { mPendingIntent = PendingIntent.getService(this, 0, new Intent( "com.action.ticker"), 0); } if (mAlarmManager == null) { mAlarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); } mAlarmManager.cancel(mPendingIntent); }
在有新訊息時,通常會進行系統標題欄的訊息通知,在點選通知後進入應用的對應介面
程式碼片段
private void startTicker() { if (mPendingIntent == null) { mPendingIntent = PendingIntent.getService(this, 0, new Intent( "com.yqfz.bus.ticker"), 0); } if (mAlarmManager == null) { mAlarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); } mAlarmManager.cancel(mPendingIntent); mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 2 * 60000, mPendingIntent); } private void stopTicker() { if (mPendingIntent == null) { mPendingIntent = PendingIntent.getService(this, 0, new Intent( "com.yqfz.bus.ticker"), 0); } if (mAlarmManager == null) { mAlarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); } mAlarmManager.cancel(mPendingIntent); }