Get a list of registered pending intentions in the Android OS

I register alarms that I plan to execute at the moment, and there can be many alarms depending on the size of the planned list. But I have two questions that are still unclear to me:

1) How can I request an OS for pending intent registers? I need this for testing. The psudo code for what I want will be something like this:

List<PendingIntent> intentsInOS = context.getAllPendingIntentsOfType(AppConstants.INTENT_ALARM_SCHEDULE)); 

2) See the pending intention that I create, I provide the action and additional data (schedule identifier).

 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 this intention has FLAG_CANCEL_CURRENT. Will it cancel all pending intentions with the same action or will it have both one action and additional 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; } 
+7
source share
1 answer

1 How can I request an OS for pending intent registers?

I'm not sure you can, but you can check if a specific PendingIntent or not:

 private boolean checkIfPendingIntentIsRegistered() { Intent intent = new Intent(context, RingReceiver.class); // Build the exact same pending intent you want to check. // Everything has to match except extras. return (PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_NO_CREATE) != null); } 

2 Will he cancel all pending intentions using the same action, or will he have both one action and additional data?

PendingIntent all PendingIntent that are allowed equal.

What does equal mean?

The android java document says:

Determine if the two intentions are the same for the purpose of the intent permission (filtering). That is, if their action, data, type, class, and categories are the same. This does not compare the excess data included in the intent.

You can read on line 7391 here: https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/content/Intent.java

To summarize, all PendingIntent that are built in exactly the same way, except for additional ones, will be canceled.

+7
source

All Articles