我正在开发一个Android应用程序,它应该每天发出五次警报:
- 每天的时间不恒定
- 警报发出后,我将安排下一次警报。
我的问题是:警报通知工作1天然后停止,并且当设备重新启动两次通知不起作用时,我现在不知道如果还有其他方法可以做到这一点,任何帮助都将是非常感激!
代码: 我有这个功能来调用广播接收器
public static void Start_Notifying(Context context){
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.add(Calendar.HOUR_OF_DAY,hour);
cal.add(Calendar.MINUTE, min);
Intent intent = new Intent(context, OnetimeAlarmReceiver.class);
intent.setAction("START_NOTIFYING");
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, notify.REQUEST_CODE, intent,0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis() , pendingIntent);
SaveAlarmManager(alarmManager, pendingIntent);
}
oneTimeAlarmReciever的代码
public void onReceive(Context context, Intent intent) {
main_menu.con = context;
Notification notifyDetails;
NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notifyDetails = new Notification(R.drawable.icon,s1,System.currentTimeMillis());
PendingIntent pendingIntent = PendingIntent.getActivity(context, REQUEST_CODE, new Intent(context, prayertimes.class), 0);
notifyDetails.setLatestEventInfo(context,s2+ notify.prayName , s3, pendingIntent);
notifyDetails.sound=Uri.parse(PlaySound());
nm.notify(NOTIFICATION_ID, notifyDetails);
main_menu.notify_me();
}
notify_me()的代码
static void notify_me() {
hour =pTime.num1;
min =pTime.num2;
Start_Notifying(con);
}
在清单文件中
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
<receiver class =".OnetimeAlarmReceiver" android:name="OnetimeAlarmReceiver">
<intent-filter>
<action android:name="START_NOTIFYING" />
<action android:name="android.intent.action.BOOT_COMPLETED"> </action>
<data android:mimeType="audio/ogg" />
</intent-filter>
</receiver>
答案 0 :(得分:25)
为什么警报通知在系统重启后停止
因为警报计划在重新启动时被清除。如果您希望在重新启动后再次启动警报,则需要实现响应BroadcastReceiver
广播的RECEIVE_BOOT_COMPLETED
。 my WakefulIntentService
repo中的demo/
项目证明了这一点。