I have some trouble with PendingIntents. Every time my app is opened it will schedule some broadcasts. My Problem is that already existing PendingIntents aren't recognized.
I know that the PendingIntents and the unterlaying Intents must be created with the same parameters.
Her is my code ... started in my Launcher Activity as a Asynctask.
long nextExecute = getNextExecute(t);
if (nextExecute > System.currentTimeMillis()) {
int tt = 12;
intent = new Intent(context, BirthExecutor.class);
intent.putExtra(Target.ROW_ID, tt);
PendingIntent pending = PendingIntent.getBroadcast(context, 10, intent, PendingIntent.FLAG_NO_CREATE);
if (pending != null) {
manager.set(AlarmManager.RTC_WAKEUP, nextExecute, pending);
BirthLog log = new BirthLog("Scheduled " + t.getFirstname() + " " + t.getLastname() + " on " + df.format(nextExecute));
log_helper.insertLog(log);
} else {
BirthLog log = new BirthLog(t.getFirstname() + " " + t.getLastname() + " already active!");
log_helper.insertLog(log);
}
intent = new Intent(LogAdapter.LOG_RECEIVE_ACTION);
context.sendBroadcast(intent);
}
The exact same code is executed each time but why is the pending variable not null? It has to be !!! I have hardcoded the requestId also the putExtra is hardcoded.
EDIT
I have another function to update this schedules. Only if I execute that function the PendingIntents aren't recognized any more. I tried to use the same context object as a static reference but also that failed.
public void updateTask(Target t) {
if (t.getTime() == null || t.getRow() == null)
return;
Intent intent;
SimpleDateFormat df = new SimpleDateFormat("HH:mm dd.MM.yyyy", Locale.US);
long nextExecute = getNextExecute(t);
if (nextExecute > System.currentTimeMillis()) {
intent = new Intent(static_context, BirthExecutor.class);
intent.putExtra(Target.ROW_ID, 12);
PendingIntent pending = PendingIntent.getBroadcast(static_context, 10, intent, PendingIntent.FLAG_CANCEL_CURRENT);
if (pending != null) {
manager.set(AlarmManager.RTC_WAKEUP, nextExecute, pending);
BirthLog log = new BirthLog("Scheduled " + t.getFirstname() + " " + t.getLastname() + " on " + df.format(nextExecute));
log_helper.insertLog(log);
Log.d("birth", log.getComment());
} else {
BirthLog log = new BirthLog(t.getFirstname() + " " + t.getLastname() + " ERROR. TIME: " + df.format(nextExecute));
log_helper.insertLog(log);
Log.d("birth", log.getComment());
}
intent = new Intent(LogAdapter.LOG_RECEIVE_ACTION);
static_context.sendBroadcast(intent);
}
}
EDIT 2
Ok it makes sence if it returns a non-null result if that pending-intent is already scheduled. I have changed my code. Every time I rotate my phone the main activity onCreate is fired and the scheduling asynctask is executed. BUT the result is always null ?!?! It should be a non-null result if there is a scheduled pendingintent already, right?
@Override
protected Void doInBackground(Void... params) {
Intent intent;
SimpleDateFormat df = new SimpleDateFormat("HH:mm dd.MM.yyyy", Locale.US);
for (Target t : target_helper.getAllTarget()) {
long nextExecute = getNextExecute(t);
if (nextExecute > System.currentTimeMillis()) {
PendingIntent pending = getPendingIntent(context, t, false);
if (pending != null) {
BirthLog log = new BirthLog(t.getFirstname() + " " + t.getLastname() + " already active!");
log_helper.insertLog(log);
} else {
manager.set(AlarmManager.RTC_WAKEUP, nextExecute, pending);
BirthLog log = new BirthLog("Scheduled " + t.getFirstname() + " " + t.getLastname() + " on " + df.format(nextExecute));
log_helper.insertLog(log);
}
intent = new Intent(LogAdapter.LOG_RECEIVE_ACTION);
context.sendBroadcast(intent);
}
}
return null;
}
private PendingIntent getPendingIntent(Context context, Target t, boolean update) {
Intent intent = new Intent(context, BirthExecutor.class);
intent.putExtra(Target.ROW_ID, t.getRow());
if (update) {
return PendingIntent.getBroadcast(context, t.getRow().intValue(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
} else {
return PendingIntent.getBroadcast(context, t.getRow().intValue(), intent, PendingIntent.FLAG_NO_CREATE);
}
}
The documentation is incorrect. If you use PendingIntent.FLAG_NO_CREATE
when calling PendingIntent.getBroadcast()
it will return null
if no matching PendingIntent
is found. Otherwise it will return the matching PendingIntent
.
The documentation says it is the other way around, but that's wrong :-(
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With