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
265 views
in Technique[技术] by (71.8m points)

android - How to create a persistent AlarmManager

Edit: Clarified the question based on CommonsWare's answer

We're scheduling an an alarm via AlarmManager to trigger every 60 seconds. When our application is killed our alarms seem to no longer execute. Is there a way to make these alarms persist even when the application is killed manually or by the system?

This is a problem for us because we have a widget application that displays the time. This means we need to update the time every minute. To get around the 30 minute update limit on the onUpdate method of AppWidgetProvider we use AlarmManager. It usually works pretty well, but some users have reported the time going out of sync. After talking to several of them, my suspicion is that our application is being killed manually via a task killer app or Android is itself is killing our app.

Any other alternate solutions to the root problem (keeping the time in sync in a widget) welcome as well.

Here is the code we execute to schedule our alarm:

Intent intent = new Intent(UPDATE_TIME);
PendingIntent pIntent = PendingIntent.getBroadcast(ctx,
  0 /* no requestCode */, intent, PendingIntent.FLAG_UPDATE_CURRENT );

// get alarm params
Date d = new Date();
long timeTilMinuteChange = 60*1000-d.getSeconds()*1000;
long startTime = System.currentTimeMillis() + + timeTilMinuteChange;

AlarmManager am = (AlarmManager) ctx.getSystemService(Context.
am.cancel(pIntent);
am.set(AlarmManager.RTC, System.currentTimeMillis(), pIntent);
        am.setRepeating(AlarmManager.RTC, startTime, 60000, pIntent);
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Whenever our application is killed, the AlarmManager is also killed.

AlarmManager is not killed. Your alarm, however, is canceled. If the user force-stops or task-kills you, your alarms are unscheduled. On Android 3.1+, if the user force-stops you, nothing of your code will run again until the user manually launches one of your activities.

After talking to several of them, my suspicion is that our application is being killed manually via a task killer app or Android is itself is killing our app.

Ideally, your app should not be written in such a way that Android would have any cause to get rid of you. For something like what you describe, you should either using a getBroadcast() PendingIntent pointing to a manifest-registered BroadcastReceiver, or you should be using a getService() PendingIntent pointing to an IntentService. In either case, your code will run briefly, and then your process will be eligible for reclamation by Android without issue should the need arise.

Task killers, whether manual or automatic, seem a far more likely culprit of alarms being canceled, IMHO.


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

...