I'm looking for a solution for days now. There might not even be a solution.
What happens is the following:
I got an application which schedules alarms for users, for the user to get out of bed. It's really important that the alarms "always" go off.
To be able to let the alarms go off I make use of an AlarmManager. This schedules intents to be executed on scheduled times. This works fine when the Application is open or closed with the back/home button.
When I close the app with Force Close option, the Alarm Manager alarms get cleared from the system and they will not go off.
I have tried the following things already:
START_STICKY Service
DefaultUncaughtExceptionHandler - Won't work, because there isn't an exception caught for force closing the app.
Broadcast Receiver - The issue is with the alarms being deleted.
I'm wondering if anyone has experienced the same issue with the AlarmManager class and if you found a solution or alternative to fix this problem.
p.s. I'm creating the Alarm Manager alarms like this:
Intent myIntent = new Intent(getApplicationContext(), CheckAlarmService.class);
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(),
1, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)getApplicationContext().getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
int daysTillAlarm = AlarmHelper.calculateDaysTillAlarm(alarm);
calendar.add(Calendar.DATE, 0);
calendar.set(Calendar.HOUR_OF_DAY, 15);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
EDIT:
To try and see if I wasn't the only one with the issue, I downloaded some well used alarm clocks. They all seem to have the same behavior as I do. For now I'll leave it be. They're also well rated.
Still if you might have the solution to this problem I'd like to know!
Try changing you code:
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(),
1, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
to:
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(),
1, myIntent, 0);
Also try adding:
alarmManager.cancel(pendingIntent);
before:
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
From my point of view (as Android user) this is right behavior. Imagine application which has bug leading to crash (with crash dialog), and this application scheduled some action to be started every minute. What can you do with crash dialog appearing every minute?
So I hope there is no direct answer to your question (like "just call this method").
As always, there are ugly ways - like starting several processes which track all others and restart if needed, but then your application starts to look like virus...
Related
I am trying to set two alarms which will run two different background services. I set the alarm inside the onCreate method of my activity class. But the problem is that the service classes which are extending IntentService are not getting called, i.e. their method onHandleIntent() is not getting called. This is how I set my alarms
//Creating alarm for showing notifications.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
//create an alarm for today if there is still time else schedule alarm for tomorrow.(Handled inside the one time alarm class).
//FIRST ALARM..............
Intent intent = new Intent(ActionBarTabActivity.this, ScheduleOneTimeAlarmForToday.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
//SECOND ALARM.............
Intent i = new Intent(ActionBarTabActivity.this,RemoteNotificationService.class);
PendingIntent pi = PendingIntent.getService(getApplicationContext(), 111, i, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(), 1000 * 60, pi);
I have declared the services inside the minifest file as below
<service android:name="com.nss.common.services.ScheduleOneTimeAlarmForToday" />
<service android:name="com.nss.common.services.RemoteNotificationService" />
Also the alarms run properly on my old samsung phone but when I test it on my new Asus Zenfone or any other new phone, it doesn't show up.
Edit:
My logcat shows this:
10-19 12:25:05.605 634-744/? V/AlarmManager﹕ triggered: flg=0x10000000 cmp=com.nss.zobbers/com.nss.common.services.ScheduleOneTimeAlarmForToday Pkg: com.nss.zobbers
10-19 12:25:06.846 634-744/? V/AlarmManager﹕ triggered: cmp=com.nss.zobbers/com.nss.common.services.RemoteNotificationService Pkg: com.nss.zobbers
So I don't get it, my alarm is triggered but the service it needs to call doesn't get called? I have tried many posts but couldn't find the error. Please help, thanks in advance.
I made it to work, but I won't accept it as my answer as maybe someone will tell the exact reason why it doesn't work. Well instead of directly calling services via alarmManager, I modified my code to call a broadcast receiver which then calls the respective service. Now it seems to work perfectly on different devices.
This is my first post here. I have searched all around for this topic, but nothing seems to help. When the code below gets executed, the alarm still goes off, when it should not.. meaning that I cannot cancel the alarm, making my app useless for API's lower than 21. Any idea why? Please help! I should mention that when I try this code using an API22 emulator(nexus one) from Android Studio it works, but when I use it with an API18 emulator(samsung s3) from genymotion it does not.
Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 8, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
am.cancel(pendingIntent);
Looks like you're just not giving set() enough time to set the alarm before you're calling cancel().
Put the call to cancel() in a separate code block so that it's not being called immediately after set(), and it should work.
I'm attempting to use AlarmManager to schedule a delayed check in my app. (Specifically, N minutes after a user approaches a location, I want to check whether they're still there, and if so send them a notification.)
I've implemented this by checking to see whether they've entered the region in my location update receiver and, if they have, scheduling like so:
Intent geofenceIntent = new Intent(context, GeofenceReceiver.class)
// ...intent contents not important...
PendingIntent pi = PendingIntent.getBroadcast(context, 0, geofenceIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar c = Calendar.getInstance();
c.add(Calendar.SECOND, getGeofenceDelaySeconds());
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);
Log.v("Scheduling notification check for "+c.getTime());
When the battery level is high (say, 50%), everything works like a charm. But when it's low (say, 10%), I get location updates, they schedule the alarm seemingly-successfully, but that alarm never actually triggers!
What gives? Does Android stop sending certain types of updates when it's trying to conserve power? How can I work around this (short of actually keeping my app active for the duration of the delay, which causes obvious issues with battery life)?
It turns out that this is related to the use of the real-time clock.
Although I could not find the documentation it quotes (it's not in AlarmManager), this StackOverflow answer suggests that AlarmManager.RTC_WAKEUP alarms do not trigger if the phone is in power-saving mode. AlarmManager.ELAPSED_REALTIME_WAKEUP alarms do not seem to suffer this problem, so I was able to fix the issue by switching to:
Intent geofenceIntent = new Intent(context, GeofenceReceiver.class)
// ...intent contents not important...
PendingIntent pi = PendingIntent.getBroadcast(context, 0, geofenceIntent, PendingIntent.FLAG_UPDATE_CURRENT);
long millis = SystemClock.elapsedRealtime() + 1000 * getGeofenceDelaySeconds();
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, millis, pi);
I've searched for 3 days now but didn't find a solution or similar problem/question anywhere else. Here is the deal:
Trigger in 1 hour -> works correct
Trigger in 2 hours -> Goes of in 1:23
Trigger in 1 day -> Goes of in ~11:00
So why is the AlarmManager so unpredictable and always too soon? Or what am I doing wrong? And is there another way so that it could work correctly?
This is the way I register my PendingIntent in the AlarmManager (stripped down):
AlarmManager alarmManager = (AlarmManager)parent.getSystemService(ALARM_SERVICE);
Intent myIntent = new Intent(parent, UpdateKlasRoostersService.class);
PendingIntent pendingIntent = PendingIntent.getService(parent, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//Set startdate of PendingIntent so it triggers in 10 minutes
Calendar start = Calendar.getInstance();
start.setTimeInMillis(SystemClock.elapsedRealtime());
start.add(Calendar.MINUTE, 10);
//Set interval of PendingIntent so it triggers every day
Integer interval = 1*24*60*60*1000;
//Cancel any similar instances of this PendingIntent if already scheduled
alarmManager.cancel(pendingIntent);
//Schedule PendingIntent
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, start.getTimeInMillis(), interval, pendingIntent);
//Old way I used to schedule a PendingIntent, didn't seem to work either
//alarmManager.set(AlarmManager.RTC_WAKEUP, start.getTimeInMillis(), pendingIntent);
It would be awesome if anyone has a solution. Thanks for any help!
Update:
2 hours ago it worked to trigger it with an interval of 2 hours, but after that it triggered after 1:20 hours. It's getting really weird. I'll track the triggers down with a logfile and post it here tomorrow.
Update:
The PendingIntent is scheduled to run every 3 hours. From the log's second line it seems like an old scheduled PendingIntent is still running:
[2012-5-3 2:15:42 519] Updating Klasroosters
[2012-5-3 4:15:15 562] Updating Klasroosters
[2012-5-3 5:15:42 749] Updating Klasroosters
[2012-5-3 8:15:42 754] Updating Klasroosters
[2012-5-3 11:15:42 522] Updating Klasroosters
But, I'm sure I cancelled the scheduled PendingIntent's before I schedule a new one. And every PendingIntent isn't recreated in the same way, so it should be exactly the same. If not , this threads question isn't relevant anymore.
When using a calendar are you taking into account that the calendar uses the time right down to Milli seconds. Maybe you should set the Milli second field and the seconds field to zero so it's going of on the dot.
Also for a day it would be easier to use this
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(0);
cal.add(Calendar.DAY_OF_MONTH, 1);
Also when you use getInstance doesn't that set the calendars time to the time it was created so there shouldn't be any need to set the time again right?
Rewrite: I eventually saw your error, but unpredictably.
I did changed this:
PendingIntent.getService(parent, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
to this:
PendingIntent.getService(parent, 0, myIntent, PendingIntent.FLAG_CANCEL_CURRENT);
under the same assumption as you that somehow an old intent is broadcasting. I haven't seen the fluke since...
Also the only times I saw it were during my initial call. Another approach could be to track a current and a previous Calendar object, if the interval isn't what you expected then ignore this "early" broadcast. (While this method seems redundant considering how the alarm should work, it helps prevent those extraneous calls considering how the alarm is working...)
Hope that helps, I'll let you know if I find anything else.
I know this question is a bit old, but I had this same problem myself. I found out that if I tried to declare the Calendar variable outside of the method, it wouldn't play nicely and the alarms would fire early. Because your class is stripped down it is hard to tell exactly where you're calling the calendar instance.
If I set it up as such, then it would fire right on time:
protected void nextAlarm(Context context, int seconds){
Calendar nextAlarm = Calendar.getInstance();
Intent intent = new Intent(context, MyClass.class);
PendingIntent pending = PendingIntent.getBroadcast(context, MainActivity.REPEATING_ALARM, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager amanager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
nextAlarm.add(Calendar.SECOND, seconds);
amanager.set(AlarmManager.RTC_WAKEUP, nextAlarm.getTimeInMillis(), pending);
}
Make sure your service's onStartCommand returns START_NOT_STICKY, otherwise it will be automatically re-attempted:
public class UpdateKlasRoostersService extends Service {
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
buildUpdate();
return START_NOT_STICKY;
}
}
I'm usign an Alarm Manager to update a widget with a Service. I've two different questions.
First question: I'm calling the service with Alarm Manager's intent. Like this:
alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, MyService.class);
pi = PendingIntent.getService(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
context.startService(new Intent(context, MyService.class));
Long repeat = Long.parseLong(prefs.getString("update_preference", "600"));
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis(), 1000*repeat, pi);
Is it wrong?
It looks and works right. But when I have looked at working services, I can't see my service name in the list. Perhaps it's not running as a single/seperate service. Just saw application name (not as a service). I'm not sure how to seperate or does it matter?
Another question: Over long time, running application, which controls widgets update, is closed somehow (manually or by a task killer). Of course Alarm Manager gonna stop and widget's functions gonna stop too. For example button clicking.
But, Twitter solved this problem. While the widget is active, if I close the main application (Twitter) -which controls widget- than click the widget, somehow widget triggering application and it starts again well. So buttons work properly. How is that possible?
Any help would be appreciated.
You dont need to do context.startservice that what the pending intent is for, if you want the service to run right away the first time just set it to run at the current time then set the interval from the current time.
You are also setting 2 different types of repeating when you don't need to setRepeating is strict where setInexact is not and can be adjusted by the OS when it gets fired hence the inexact in it. You want one or the other not both.
Also those intervals are very small and its going to kill the battery significantly.
It should just be this
alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, MyService.class);
pi = PendingIntent.getService(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
Long repeat = Long.parseLong(prefs.getString("update_preference", "600"));
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,
Calendar.getInstance().getTimeInMillis(), 1000*repeat, pi);
It's good that your application/service isn't running all the time.
In fact it doesn't need/has to run all the time for updating a widget. If you schedule an Intent with AlarmManager the application will be started when the intent is fired if it has been closed.