Android 重新启动手机后设置闹钟
我有个小问题。我使用AlarmManager在特定时间设置通知。我设置通知的时间存储在SQLLite数据库中。除了我重启手机的那一刻,它们都能正常工作。alarmManager当然会减少重复 我想问,在这种情况下,最好的解决办法是什么?我在MainActivity中设置了alarmManager,并在BroadcastReceiver中设置了通知,如下代码所示: 以下是我在MainActivity中对其的称呼:Android 重新启动手机后设置闹钟,android,broadcastreceiver,alarmmanager,notificationmanager,android-reboot,Android,Broadcastreceiver,Alarmmanager,Notificationmanager,Android Reboot,我有个小问题。我使用AlarmManager在特定时间设置通知。我设置通知的时间存储在SQLLite数据库中。除了我重启手机的那一刻,它们都能正常工作。alarmManager当然会减少重复 我想问,在这种情况下,最好的解决办法是什么?我在MainActivity中设置了alarmManager,并在BroadcastReceiver中设置了通知,如下代码所示: 以下是我在MainActivity中对其的称呼: Intent intent = new Intent(context
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);
这是广播接收机的接收方法
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事件声明BroadcastReceiver,但它总是在我启动手机时只调用空通知,而不再调用
我想问,在这种情况下,最好的解决办法是什么
注册一个BOOT\u COMPLETED
BroadcastReceiver
以调用AlarmManager
上的setRepeating()
,重新建立您的时间表
我为BOOT_COMPLETED事件声明BroadcastReceiver,但它总是在我启动手机时只调用空通知,而不再调用
BOOT_COMPLETED
BroadcastReceiver
的目标应该是重新安排您的警报。你可能想考虑使用一个单独的<代码>广播接收器< /代码>,而不是你正在使用的告警事件本身。所以你是说要使用像OnBOOTHORT之类的东西,我将得到时间,并使用ARARMMARK调用第一个广播接收器。欢迎您使用一个BroadcastReceiver
来处理这两个角色,但您需要区分启动事件和报警事件,例如通过检查传入Intent
的操作字符串。例如,使用这种方法,因为BOOT_COMPLETED
广播将有一个非null
操作字符串,而我在pendingent
中使用的Intent
和AlarmManager
的显式操作字符串将有一个null
操作字符串。非常感谢您的帮助。