PendingIntent.getBroadcast()永远不会返回null [英] PendingIntent.getBroadcast() never returns null
问题描述
我试图找到的PendingIntent
我的广播接收器
这是使用称为 AlarmManager
。
我使用 PendingIntent.getBroadcast()
使用它被称为具有非常相同的参数,但它永远不会返回null。
公开为MyObject(上下文的背景下,AnotherObject对象){ 我的意图;
I =新意图(背景下,MyReceiver.class);
i.putExtra(AnotherObject.SOMETHING,串);
i.putExtra(AnotherObject.SOME_BOOL,真); PI的PendingIntent = PendingIntent.getBroadcast(背景下,0,I,PendingIntent.FLAG_NO_CREATE); 如果(PI == NULL){
Log.v(帮助,的PendingIntent为空);
}
其他{
Log.v(帮助,的PendingIntent不为空);
}
}
下面是我的测试驱动程序:
=该新MyObject来(这一点,对象);LogUtil.startAlarm(这一点,对象);该=新LogUtil(这一点,对象);
这是 startAlarm
:
公共静态无效startAlarm(背景信息,AnotherObject对象){ 我的意图;
I =新意图(即,MyReceiver.class);
i.putExtra(AnotherObject.SOMETHING,串);
i.putExtra(AnotherObject.SOME_BOOL,真); 长间隔= 60 * 1000; //分钟
长的第一= SystemClock.elapsedRealtime()+区间; PI的PendingIntent = PendingIntent.getBroadcast(即,0,I,PendingIntent.FLAG_UPDATE_CURRENT); //安排报警
AlarmManager AM2 =(AlarmManager)that.getSystemService(Context.ALARM_SERVICE);
am2.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,第一次,间隔,PI);
Log.v(帮助,启动报警);
}
下面是我的日志输出:
十一月9日至2日:18:54.330:VERBOSE /帮助(9621)的PendingIntent不为空
十一月九日至2日:18:54.330:VERBOSE /帮助(9621):开始报警
十一月九日至2日:18:54.330:VERBOSE /帮助(9621)的PendingIntent不为空
为什么它说这是从来没有空,甚至在我启动报警?不应该 getBroadcast()
返回NULL?
我试图找到我的BroadcastReceiver的的PendingIntent被使用AlarmManager调用。
块引用>呃,为什么?
我使用PendingIntent.getBroadcast()使用,它调用了同样的参数,但它永远不会返回null。
块引用>这不应该。这将创建一个
的PendingIntent
如果没有一个等效的底层意图
。有人可能会说,该方法的名称应该是createBroadcast()
来使这一点更加清晰。I am trying to find the
PendingIntent
of myBroadcastReceiver
that was called using anAlarmManager
.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 underlyingIntent
. One could argue that the method name ought to becreateBroadcast()
to make that point clearer.这篇关于PendingIntent.getBroadcast()永远不会返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!