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.
Related
I am trying to setup a Alaram below, however sometimes it fires normally and sometimes it does not and it only fires when i "open" the app. so say if alarm was schedule for 6pm, and then when i open my app at 7pm then it will fire. any ideas?
Intent intent = new Intent(AndroidApp.Context, typeof(AlarmHandler));
PendingIntent pendingIntent = PendingIntent.GetBroadcast(AndroidApp.Context, uniqueMessageId, intent, PendingIntentFlags.UpdateCurrent | PendingIntentFlags.Immutable);
AlarmManager alarmManager = AndroidApp.Context.GetSystemService(Context.AlarmService) as AlarmManager;
alarmManager.SetExactAndAllowWhileIdle(AlarmType.RtcWakeup, triggerTime, pendingIntent);
Switching alarmManager.SetExactAndAllowWhileIdle to alarmManager.SetAlarmClock Fixed it for me. Hope it can help someone else.
Documentation: https://developer.android.com/reference/android/app/AlarmManager#setAlarmClock(android.app.AlarmManager.AlarmClockInfo,%20android.app.PendingIntent)
I'm working on an app that involves an AlarmManager, and I can't seem to get it to fire. I was using a Handler originally, but I switched over to the AlarmManager so that I can wake the phone up from sleep.
Here's what I have so far:
int timeBetweeninMillis = 3 * 60000;
if (ManagerService.serviceRunning) {
Intent alarmIntent = new Intent(AlarmReceiver.ACTION_RECEIVE);
PendingIntent alarmPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 400, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mAlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, (SystemClock.elapsedRealtime() + timeBetweeninMillis), alarmPendingIntent);
}
The AlarmManager either won't fire or will fire extremely late (like 10 minutes after).
The app is written with API 16 and is being tested on a phone with API 19.
Thank you for your help!
There's an issue with new PendingIntents recycling existing PendingIntents that might cause issues with AlarmManager.
To workaround that, I always add a unique value to all my PendingIntents, this one-liner does the trick in most cases:
Intent alarmIntent = new Intent(AlarmReceiver.ACTION_RECEIVE);
alarmIntent.setData(Uri.parse("custom://" + System.currentTimeMillis())); // MAKE INTENT UNIQUE
PendingIntent alarmPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 400, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mAlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, (SystemClock.elapsedRealtime() + timeBetweeninMillis), alarmPendingIntent);
Also, I don't know what's ManagerService.serviceRunning in your code, but I would add some log to the else case to make sure you're actually running that code block.
EDIT:
try using mAlarmManager.setExactAndAllowWhileIdle instead of mAlarmManager.set this will force AlarmManager to call your code exactly when requested, and prevent device idle time from interfering with the alarm.
See:
https://developer.android.com/reference/android/app/AlarmManager.html#setExactAndAllowWhileIdle(int, long, android.app.PendingIntent)
This problem seems a little bit odd, but if someone as encounter something like this, please help me...
I created an Alarm Scheduler, that sends an alarm to the user using AlarmManager, through this code:
Intent intent = new Intent(context, AlarmReceiver.class);
intent.putExtra("tk_alert_id", lastAlertId.getId()+"");
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, idRandom, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, date.getTimeInMillis(), pendingIntent);
The problem is that, sometimes, I receive this alarm on my AlarmReceiver (BroadcastReceiver) at a wrong time, as you can see in the image bellow:
image, and I can't figure out what's the problem... I checked the time for date and was set as "2015-05-27 17:00:00", but it was received a little minutes earlier (around 16:57) ...
Does anyone knows what kind of problem I am encountering here?
For API levels <19 you should use AlarmManager.setRepeating() and your alarms will trigger exactly at specified time.
Api levels >=19 and above this no longer works. There was change in android so that all repeating alarms are inexact.
So if you would like to achieve exact repeating alarm use AlarmManager.setExact().
See this question for more info.
Edit
For your purpose (a one-off alarm, at a precise time) use alarmManager.setExact(....). See docs
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...
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.