如何获得已经待处理的PendingIntent的Intent附加信息? [英] How can I get the Intent extras for a PendingIntent that is already pending?
问题描述
我已经为AlarmManager
安排了PendingIntent
.当AlarmManager
发送Intent
时,我能够getExtras()
最初发送的所有数据.
I have scheduled a PendingIntent
with AlarmManager
. When the AlarmManager
delivers the Intent
, I am able to getExtras()
all the data that I originally sent.
但是,有时在AlarmManager
触发之前,我会获得其他要通过Intent
传递的数据.我的想法是,我将得到Intent
就像要取消它一样,但是在取消它之后,更新附加内容并用AlarmManager
重新安排它,如下所示:
However, sometimes I get additional data that I want to pass with the Intent
, before the AlarmManager
fires. My thought was, I would just get the Intent
like I was going to cancel it, but then after cancelling it, update the extras and reschedule it with the AlarmManager
, like this:
Intent i=new Intent(this, MyReceiver.class);
Bundle b = i.getExtras();
PendingIntent pi=PendingIntent.getBroadcast(this, id,i, 0);
if (b == null) b = i.getExtras(); // in case I can't get it before calling getBroadcast
// now add a key to b, put it in a new intent, schedule it, and cancel the old one?
我以与我先前安排的ID相同的ID呼叫getBroadcast()
.唯一的问题是,当我调用getExtras()
时,它总是返回null.这似乎与我经常看到的问题相反,在该问题中会缓存Intent
,这是不希望的.在这种情况下,我实际上想获取缓存的Intent
并为其获取值,并且由于Intent
处于挂起状态,因此它应该仍然存在.
I'm calling getBroadcast()
with the same id as I schedule it with earlier. The only problem is, when I call getExtras()
it always returns null. This seems to be kind of the reverse of the problem I have seen frequently where an Intent
will be cached and that is not desired. In this case I actually want to get the cached Intent
and get the value for it, and since the Intent
is pending it should still be there.
我该怎么做?
我曾经尝试过的一些想法.一个围绕Intent.fillIn()
函数,我也许可以用它来表示它应该(或不应该)覆盖这些额外内容,以便我可以检索原始内容.
A couple of ideas I have had and/or tried. One revolves around the Intent.fillIn()
function, could I perhaps use this to indicate that it should (or should not) overwrite the extras so that I can retrieve the original ones.
再看一下,我看到PendingIntent
具有writeToParcel()
.我可以使用它来访问附加的Intent
的其他功能吗?我的尝试失败,并在readParcel上出现readBundle: bad magic number
异常:
Looking a bit further, I see that PendingIntent
has writeToParcel()
. Can I use this to get access to the extras of the attached Intent
? My attempt failed with readBundle: bad magic number
exception on readParcel:
PendingIntent pi=PendingIntent.getBroadcast(this, id,i, 0);
Parcel out = Parcel.obtain();
pi.writeToParcel(out, 0);
i.readFromParcel(out);
我怀疑这可能是因为我正在输出PendingIntent
,但试图将其读回为Intent
.
I suspect that maybe this is because I am outputting a PendingIntent
but trying to read it back as an Intent
.
另一个想法是,我可以在PendingIntent
上调用send()使其立即交付,并传递一个带有新密钥的新Intent
.然后,在处理程序中,我将不得不遍历意向之外的所有键.如果还没来得及进行实际处理,则需要将PendingIntent
重新发布到AlarmManager
.
The other idea is that I could call send() on the PendingIntent
to cause it to be immediately delivered, passing a new Intent
with a new key to add. Then in the handler I would have to iterate through all keys in the extras of the intent. It would then need to re-issue the PendingIntent
to the AlarmManager
if it wasn't time yet to do the actual processing.
推荐答案
好,我终于找到了解决此问题的方法.
Ok, I finally found a solution to this problem.
似乎不可能从PendingIntent
获取Intent
,但是可以在传递给getBroadcast()
的Intent
中添加其他内容-重要的是,最后一个参数标志,请设为零,以免覆盖现有的附加功能.
It doesn't seem to be possible to get the Intent
from a PendingIntent
, but it is possible to add extras in the Intent
passed to getBroadcast()
- it is important that the last parameter, the flags, be zero so that it won't overwrite the existing extras.
此外,附加内容还包含名称/值对,因此添加的新键与现有Intent
中的键不匹配非常重要.这意味着必须在完成后调用removeExtra()
才能在下一次清除键之前.然后以PendingIntent
为自己的目标调用send()
,然后在收到意图后,就可以访问新的和旧的附加功能.然后可以将这两个附加项收集到一个新的Intent
中,然后可以调用AlarmManager
进行重新计划.
Also, the extras contain name/value pairs, and it is important that the new key added not match a key in the existing Intent
. This means that removeExtra()
must be called when done to get rid of the key before next time. Then call send()
on the PendingIntent
targeting oneself, and then when receiving the intent it is possible to access both the new and old extras. Both extras can then be collected into a new Intent
and then the AlarmManager
can be called to reschedule.
这篇关于如何获得已经待处理的PendingIntent的Intent附加信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!