I am trying to find t开发者_如何学JAVAhe 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?
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.
精彩评论