附表通知不会触发 [英] Schedule notification not triggered

查看:126
本文介绍了附表通知不会触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我希望它推出一个通知每隔x天(由用户pdefined $ P $)的应用程序。对于我使用这个code:
类扩展广播接收器:

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);
     }

    }

和此功能中的其他类推出这项previous类:

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);

    }

我将它推出每半小时的通知,但我从未有过。
这是怎么回事这个code?

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

非常感谢你。

推荐答案

SystemClock.elapsedRealtime()表示现在到毫秒,所以有一种可能性,即通过它得到一个机会来运行的时候,triggerAtTime也就过去了(不知道AlarmManager如何处理这种情况下)。尝试将其延迟了几秒钟,看它是否运行正常: SystemClock.elapsedRealtime()+ 10 * 1000

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.

在另一方面,大的间隔重复报警(如天)是不可靠的:如果进程被杀掉,或抛出一个异常,或设备重启,那么它的警报将被清除。一种选择(虽然不是100%防弹)将安排从previous各一个下一个报警并保存它将运行到共享preferences例如时间。然后,每个应用程序启动时,请检查下一次运行是有效的(即在未来),如果没有安排新的警报。

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.

这篇关于附表通知不会触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆