PendingIntent and AlarmManager
I am trying to broadcast 20 seconds later and receive an extended receiver broadcast ExtendedReceiver
.
Here is my function that raises an alarm and sets PendingIntent
to exit after 20 seconds:
public void alert() {
GregorianCalendar cal = new GregorianCalendar();
cal.add(Calendar.SECOND, 20);
Intent i = new Intent(this, ExtendedReceiver.class);
int _uid = (int) System.currentTimeMillis();
PendingIntent pi = PendingIntent.getBroadcast(this, _uid, i, PendingIntent.FLAG_ONE_SHOT);
AlarmManager am = (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pi);
Log.i("Title", "Alarm has been set");
}
Here is the ExtendedReceiver class:
public class ExtendedReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("Title", "Broadcast received");
}
}
I get my first log message: Alarm has been set
but I don't get my second log message. I'm not sure where the problem is (first day on Android)
source to share
If I can, this is what I always do.
change your
PendingIntent pi = PendingIntent.getBroadcast(this, _uid, i, PendingIntent.FLAG_ONE_SHOT);
to
PendingIntent pi = PendingIntent.getBroadcast(this, _uid, new Intent(EXTENDED_RECEIVER_ACTION), PendingIntent.FLAG_ONE_SHOT);
and important: register yours ExtendedReceiver
like this:
...
registerReceiver(new ExtendedReceiver() , new IntentFilter(EXTENDED_RECEIVER_ACTION));
PS: EXTENDED_RECEIVER_ACTION is a string and don't forget to unregister your receiver.
further docs here
hope this helps :)
source to share