我有一個小問題。我使用AlarmManager在特定時間設置了我的通知。我爲通知設置的時間存儲在SQLLite數據庫中。除了我重啓手機的那一刻,他們都工作得很好。 alarmManager當然會放棄他們的重複。重新啓動後設置鬧鐘電話
我想問一下這種情況下最好的解決方案是什麼?我有我的alarmManager在MainActivity設置和設置我的裏面廣播接收器的通知,你可以在下面的代碼中看到:
這是我如何把它從MainActivity:
Intent intent = new Intent(context, MyReceiver.class);
intent.putExtra(EXTRA_TITLE, title);
intent.putExtra(EXTRA_COUNT, count);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, count, intent, PendingIntent.FLAG_CANCEL_CURRENT);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), WEEK_LENGTH_MS, pendingIntent);
這裏是廣播接收器的方法的onReceive
public void onReceive(Context context, Intent intent)
{
nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence from = context.getString(R.string.app_name);
CharSequence message = intent.getStringExtra(DayActivity.EXTRA_TITLE);
Intent intentNotification = new Intent(context,DayActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, intent.getIntExtra(DayActivity.EXTRA_COUNT,0), intentNotification, 0);
Notification notif = new Notification(R.drawable.notification_logo,context.getString(R.string.app_name), System.currentTimeMillis());
notif.setLatestEventInfo(context, from, message, contentIntent);
notif.defaults |= Notification.DEFAULT_LIGHTS;
notif.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;
nm.notify(intent.getIntExtra(DayActivity.EXTRA_COUNT,0), notif);
}
我聲明廣播接收器的BOOT_COMPLETED事件,但它總是來電我啓動手機,從來沒有更多的時候只是空的通知。
所以你說要使用像OnBootReceiver的東西,我會得到時間,並使用alarmManager調用第一個BroadcastReceiver? – 2014-10-18 13:57:55
@JanOmacka:是的。歡迎您有一個'BroadcastReceiver'處理這兩個角色,但您需要區分引導事件和警報事件,例如通過檢查傳入的「Intent」的操作字符串。例如,[這個示例應用程序](https://github.com/commonsguy/cw-omnibus/tree/master/AlarmManager/WakeCast)使用該方法,因爲BOOT_COMPLETED廣播將具有非'null'動作字符串,而我在'PendingIntent'中用'AlarmManager'使用的顯式'Intent'將有一個'null'動作字符串。 – CommonsWare 2014-10-18 14:02:37
非常感謝您的幫助。 – 2014-10-18 14:10:20