I register alarms which I schedule to execute at given time, and it can be many alarms depending on the size of the scheduled list. But I have two questions which remains unclear to me:
1) How can I query the OS for the Pending Intents I registers? I need this for testing. The psudo code for what I want would be something like this:
List<PendingIntent> intentsInOS = context.getAllPendingIntentsOfType(AppConstants.INTENT_ALARM_SCHEDULE));
2) See the pending intent I create, I provide an action and extra data (the schedule id).
private Intent getSchedeuleIntent(Integer id) {
Intent intent = new Intent(AppConstants.INTENT_ALARM_SCHEDULE);
intent.putExtra(AppConstants.INTENT_ALARM_SCHEDULE_EXTRA, id);
return intent;
}
But we also say that the intent have FLAG_CANCEL_CURRENT. Will it cancel all pending intents with same action, or does it have to both same action AND extra data?
PendingIntent pendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), 0, getSchedeuleIntent(schedule.id), PendingIntent.FLAG_CANCEL_CURRENT);
My code
@Override
public void run() {
List<ScheduledLocation> schedules = dbManager.getScheduledLocations();
if(schedules == null || schedules.isEmpty()){
return;
}
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
//alarmManager.
// we need to get the number of milliseconds from current time till next hour:minute the next day.
for(ScheduledLocation schedule : schedules){
long triggerAtMillis = DateUtils.millisecondsBetweenNowAndNext(now, schedule.hour, schedule.minute, schedule.day);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), 0, getSchedeuleIntent(schedule.id), PendingIntent.FLAG_CANCEL_CURRENT);
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtMillis, MILLISECONDS_IN_WEEK, pendingIntent);
}
// List<PendingIntent> intentsInOS = context.getAllPendingIntentsOfType(AppConstants.INTENT_ALARM_SCHEDULE));
}
private Intent getSchedeuleIntent(Integer id) {
Intent intent = new Intent(AppConstants.INTENT_ALARM_SCHEDULE);
intent.putExtra(AppConstants.INTENT_ALARM_SCHEDULE_EXTRA, id);
return intent;
}
1 How can I query the OS for the Pending Intents I registers?
I’m not sure you can, but you can check if a specific
PendingIntentis registered or not like this :2 Will it cancel all pending intents with same action, or does it have to both same action AND extra data?
It will cancel all the
PendingIntentthat are resolved to be equal.What exactly does equal means ?
The java doc of android says:
You can read at the 7391 line here : https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/content/Intent.java
To sum up, all
PendingIntentthat are build exactly the same except extras will be cancelled.