2

I am trying to find the PendingIntent of my BroadcastReceiver that was called using an AlarmManager.

I am using PendingIntent.getBroadcast() using the very same arguments that it was called with, however it never returns null.

public MyObject(Context context, AnotherObject object) {

    Intent i;
    i = new Intent(context, MyReceiver.class);
    i.putExtra(AnotherObject.SOMETHING, "string");
    i.putExtra(AnotherObject.SOME_BOOL, true);

    PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, PendingIntent.FLAG_NO_CREATE);

    if (pi == null) {
        Log.v("help", "PendingIntent is null");
    }
    else {
        Log.v("help", "PendingIntent is not null");
    }
}

Here is my test driver:

that = new MyObject(this, object);

LogUtil.startAlarm(this, object);

that = new LogUtil(this, object);

This is startAlarm:

public static void startAlarm(Context that, AnotherObject object) {

    Intent i;
    i = new Intent(that, MyReceiver.class);
    i.putExtra(AnotherObject.SOMETHING, "string");
    i.putExtra(AnotherObject.SOME_BOOL, true);

    long interval = 60 * 1000; // Minute
    long first = SystemClock.elapsedRealtime() + interval;

    PendingIntent pi = PendingIntent.getBroadcast(that, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);

    // Schedule the alarm
    AlarmManager am2 = (AlarmManager) that.getSystemService(Context.ALARM_SERVICE);
    am2.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, first, interval, pi);
    Log.v("help", "Started alarm");
}

Here is my log output:

09-02 11:18:54.330: VERBOSE/help(9621): PendingIntent is not null
09-02 11:18:54.330: VERBOSE/help(9621): Started alarm
09-02 11:18:54.330: VERBOSE/help(9621): PendingIntent is not null

Why does it say it's never null, even before I start the alarm? Shouldn't getBroadcast() return null?

Tyler
  • 19,113
  • 19
  • 94
  • 151

1 Answers1

3

I am trying to find the PendingIntent of my BroadcastReceiver that was called using an AlarmManager.

Um, why?

I am using PendingIntent.getBroadcast() using the very same arguments that it was called with, however it never returns null.

It's not supposed to. It will create a PendingIntent if there is not already one for an equivalent underlying Intent. One could argue that the method name ought to be createBroadcast() to make that point clearer.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    I am doing this to see if my alarm has been scheduled. Could you please explain 'Returns an existing or new PendingIntent matching the given parameters. May return null only if FLAG_NO_CREATE has been supplied.' from the [Android reference](http://developer.android.com/reference/android/app/PendingIntent.html) of getBroadcast()? I thought that it would return null if it does not exist. – Tyler Sep 01 '11 at 17:47
  • @Styler: "I am doing this to see if my alarm has been scheduled." -- make note of this in your own persistent store, such as a file or database. "I thought that it would return null if it does not exist" -- I would agree, though in the source code, I do not see where it actually has this behavior. :-( Tracking your alarm yourself via a file or database will be more reliable IMHO. – CommonsWare Sep 01 '11 at 18:00