未触发计划通知

发布于 2024-11-29 10:37:56 字数 1565 浏览 0 评论 0原文

我有一个应用程序,我希望它每 x 天启动一次通知(由用户预定义)。为此,我使用这段代码: 类扩展了广播接收器:

public class OnAlarmReceiver extends BroadcastReceiver{
    @Override
     public void onReceive(Context context, Intent intent) {


        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);

        int icon = R.drawable.icon;
        CharSequence tickerText = "Votre vidange approche";
        long when = System.currentTimeMillis();

        Notification notification = new Notification(icon, tickerText, when);


        CharSequence contentTitle = "Notification";
        CharSequence contentText = "Vérifier votre kilométrage";
        Intent notificationIntent = new Intent(context, Acceuil.class);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

        final int HELLO_ID = 1;

        mNotificationManager.notify(HELLO_ID, notification);
     }

    }

另一个类中的此函数用于启动上一个类:

public void repeating() {
        AlarmManager mgr=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        Intent i=new Intent(context, OnAlarmReceiver.class);
        PendingIntent pi=PendingIntent.getBroadcast(context, 0, i, 0);
        mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 1800000, pi);

    }

我将其设置为每半小时启动一次通知,但我从未收到过它。 这段代码是怎么回事?

非常感谢。

I have an application that i want it to launch a notification every x days(predefined by user). For that I'm using this code:
Class extends broadcast receiver:

public class OnAlarmReceiver extends BroadcastReceiver{
    @Override
     public void onReceive(Context context, Intent intent) {


        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);

        int icon = R.drawable.icon;
        CharSequence tickerText = "Votre vidange approche";
        long when = System.currentTimeMillis();

        Notification notification = new Notification(icon, tickerText, when);


        CharSequence contentTitle = "Notification";
        CharSequence contentText = "Vérifier votre kilométrage";
        Intent notificationIntent = new Intent(context, Acceuil.class);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

        final int HELLO_ID = 1;

        mNotificationManager.notify(HELLO_ID, notification);
     }

    }

And this function in the other class to launch this previous class:

public void repeating() {
        AlarmManager mgr=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        Intent i=new Intent(context, OnAlarmReceiver.class);
        PendingIntent pi=PendingIntent.getBroadcast(context, 0, i, 0);
        mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 1800000, pi);

    }

I set it to launch every half hour the notification but i never had it.
What's going on with this code?

Thank you very much.

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

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

发布评论

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

评论(1

蒲公英的约定 2024-12-06 10:37:56

SystemClock.elapsedRealtime() 表示“现在”,精确到毫秒,因此当它有机会运行时,triggerAtTime 可能已经过去(不确定 AlarmManager 如何处理这种情况) )。尝试延迟几秒钟,看看它是否正常运行:SystemClock.elapsedRealtime() + 10 * 1000

另一方面,大间隔(例如几天)重复警报是不可靠的:如果您的进程被终止,或引发异常,或者设备重新启动,那么它的警报将被清除。一种替代方案(尽管不是 100% 万无一失)是安排前一个警报的下一个警报,并将其运行的时间节省到共享首选项等。然后,每次应用程序启动时,检查下一次运行时间是否有效(即将来),如果不有效则安排新的警报。

SystemClock.elapsedRealtime() means 'now' up to the millisecond, so there is a possibility that by the time it gets a chance to run, the triggerAtTime will have passed (not sure how AlarmManager handles this case). Try delaying it a few seconds to see if it runs properly: SystemClock.elapsedRealtime() + 10 * 1000.

On another note, repeating alarms with large intervals (such as days) are unreliable: if you process is killed, or throws an exception, or the device is rebooted, then it's alarms will be cleared. One alternative (although not 100% bulletproof) would be to schedule each next alarm from the previous one and save the time it will run to shared preferences for example. Then each time your application starts, check if the next run time is valid (i.e., in the future), and if not schedule a new alarm.

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