I have problems with PendingIntents. Every time my application opens, he plans to schedule some broadcasts. My problem is that existing PendingIntents are not recognized.
I know that PendingIntents and Unterlaying Intents should be created with the same parameters.
She is my code ... began in my activity of Launcher as 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);
}
Exactly the same code is executed every time, but why is the variable pending the solution not null? It must be !!! I have hardcoded the requestId and putExtra is hardcoded.
EDIT
.
, PendingIntents . , , .
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);
}
}
2
, , .
. , , onCreate . null?!?! , , ?
@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);
}
}