Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
567 views
in Technique[技术] by (71.8m points)

android - Schedule notification not triggered

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...