Reputation: 21
I'm trying to set an alarm for a notification using AlarmManager. The alarms are set when user selects a list item, so I'm trying to set a separate alarm every time (with unique ID passed into the pendingIntent) a list item is selected.
The code used to set the alarm:
public static void setAlarm(Context context, Movie movie, Schedule schedule){
Intent arg = new Intent(context, NotifyService.class);
arg.putExtra(NotificationHelper.fetch_schedule_id, schedule.getId());
arg.putExtra(NotificationHelper.fetch_movie_id, movie.getId());
PendingIntent pendingIntent = PendingIntent.getService(context.getApplicationContext(), (int) schedule.getId(), arg, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
am.setExact(AlarmManager.RTC_WAKEUP, schedule.getStart().getTime(), pendingIntent);
else
am.set(AlarmManager.RTC_WAKEUP, schedule.getStart().getTime(), pendingIntent);
}
The issue I'm dealing with is that the call to set(or setExact) method is not being executed at times. It does work half of the times, but in specific cases like, when user taps on two or more list items, one after the another in less than a second, then only the first call is executed and rest of the calls are just ignored.
Also, since set/setexact methods return void there's no way I can debug if the method was called and the alarm was set. For now I have to check the adb shell dumpsys alarm everytime.
So if someone can tell me how to schedule an alarm such that it is triggered every time, no matter how frequently the method is called or even guide me in the right direction, it'd be a great help. :)
Upvotes: 2
Views: 184
Reputation: 1669
Firstly I assume that you use this code to a Samsung Device for Lollipop and Above. In this case, I had the same problem and after a lot of searching and workaround, I figured that Smart Manager of Samsung Devices in Lollipop and above may be the problem. This component can delay the Alarm Manager to goes off, it is triggered after 3 minutes, if the mobile is working on battery and with the screen closed else it alarm manager works well, that's why you see that these methods do not execute at times. Of course you can deactivate the Smart Manager for a specific app.
But in my case it didn't work, neither disabling the Smart Manager worked in 2 Samsung devices with Lollipop . What worked was to "fool" Smart Manager by refactoring the name of my application's package to contains the String "alert" or "alarm", for example com.example.alarm.myApplication. You can also refer to this link for more information.
Upvotes: 1