我开发一个Android应用程序,应该火灾报警一天五次: - 时代的每一天不是恒定的 - 报警触发后,我会安排一个报警
我的问题是:报警通知工作1天,然后停止,并在设备重启两次通知不工作,我不知道现在是否有另一种方式来做到这一点,任何帮助将是非常AP preciated!
code: 我有这个函数来调用广播reciever
公共静态无效Start_Notifying(上下文的背景下){ 日历CAL = Calendar.getInstance(); cal.setTimeInMillis(System.currentTimeMillis的()); cal.add(Calendar.HOUR_OF_DAY,小时); cal.add(Calendar.MINUTE,分钟); 意向意图=新的意图(背景下,OnetimeAlarmReceiver.class); intent.setAction(START_NOTIFYING); PendingIntent pendingIntent = PendingIntent.getBroadcast(背景下,notify.REQUEST_ code,意向,0); AlarmManager alarmManager =(AlarmManager)context.getSystemService(ALARM_SERVICE); alarmManager.set(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),pendingIntent); SaveAlarmManager(alarmManager,pendingIntent); }
oneTimeAlarmReciever的code
公共无效的onReceive(上下文的背景下,意图意图){ main_menu.con =背景; 通知notifyDetails; NotificationManager纳米=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); notifyDetails =新的通知(R.drawable.icon,S1,System.currentTimeMillis的()); PendingIntent pendingIntent = PendingIntent.getActivity(背景下,REQUEST_ code,新的意向(背景下,prayertimes.class),0); notifyDetails.setLatestEventInfo(上下文,S2 + notify.prayName,S3,pendingIntent); notifyDetails.sound = Uri.parse(PlaySound()); nm.notify(NOTIFICATION_ID,notifyDetails); main_menu.notify_me(); }
notify_me的code()
静态无效notify_me(){ 小时= pTime.num1; 分= pTime.num2; Start_Notifying(CON); }在清单文件
<使用-权限的Android:名称=android.permission.RECEIVE_BOOT_COMPLETED>< /使用-许可> <接收器类=机器人OnetimeAlarmReceiver。:NAME =OnetimeAlarmReceiver> <意向滤光器> <作用机器人:名称=START_NOTIFYING/> <作用机器人:名称=android.intent.action.BOOT_COMPLETED> < /作用> <数据机器人:MIMETYPE =音频/ OGG/> &所述; /意图滤光器> < /接收器>解决方案
为啥系统重新启动后报警通知停止
由于报警时间表清零重新启动。如果你想你的报警器重新启动后再次回升,则需要实施的BroadcastReceiver 的响应 RECEIVE_BOOT_COMPLETED 播出。该演示/ 在我的 WakefulIntentService 回购说明了这一点。
I am developing an android application that should fire an alarm five times a day: - the times in each day is not constant - after the alarm is fired I will schedule the next alarm.
My problem is : the alarm notification works for 1 day then it stops and also when the device is rebooted twice the notification doesn't work , I don't now if there is another way to do that , any help will be very appreciated!
Code: I have this function to call the broadcast reciever
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); }the code of 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(); }The code of notify_me()
static void notify_me() { hour =pTime.num1; min =pTime.num2; Start_Notifying(con); }In the manifest file
<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>解决方案
why did the Alarm notification stop after system reboot
Because alarm schedules are cleared on a reboot. If you want to have your alarms pick up again after a reboot, you will need to implement a BroadcastReceiver that responds to the RECEIVE_BOOT_COMPLETED broadcast. The demo/ project in my WakefulIntentService repo demonstrates this.
更多推荐
安卓:为什么系统重启后没有报警通知停止
发布评论