Android:为什么系统重启后警报通知停止

发布于 2024-11-29 09:17:06 字数 2330 浏览 3 评论 0原文

我正在开发一个 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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一张白纸 2024-12-06 09:17:06

为什么系统重启后警报通知就停止了

因为警报计划在重新启动时被清除。如果您希望在重新启动后再次启动闹钟,则需要实现一个响应 RECEIVE_BOOT_COMPLETED 广播的 BroadcastReceiver我的 WakefulIntentService 存储库中的 demo/ 项目演示了这一点。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文