3

I'm trying to broadcast after 20 seconds and receive the broadcast with an extended receiver ExtendedReceiver.

Here is my function that creates an alarm and sets the PendingIntent to go off 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 unsure where the problem lies (first day on Android)

Juicy
  • 11,840
  • 35
  • 123
  • 212

1 Answers1

2

If i may 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 your ExtendedReceiver like this:

...
registerReceiver(new ExtendedReceiver() , new IntentFilter(EXTENDED_RECEIVER_ACTION));

PS: EXTENDED_RECEIVER_ACTION is a String and dont forget to unregister your receiver.

further docs here

hope it helps :)

Spurdow
  • 2,025
  • 18
  • 22