如何通过PendingIntent向Broadcast发送数据?

发布于 2024-11-25 14:20:16 字数 1085 浏览 0 评论 0原文

我试图通过 PendingIntent 发送一些额外的数据,例如:

MyMessage message;
//...
Intent intent;
SmsManager sms = SmsManager.getDefault();
intent = new Intent(Constants.SENT_PLAIN);
intent.putExtra(Constants.EXTRA_RAW_ID, message.getId()); //putting long id (not -1L)
PendingIntent sentPI = PendingIntent.getBroadcast(activity, 0, intent, 0);
intent = new Intent(Constants.DELIVERED_PLAIN);
intent.putExtra(Constants.EXTRA_RAW_ID, message.getId());
PendingIntent deliveredPI = PendingIntent.getBroadcast(activity, 0, intent, 0);
sms.sendTextMessage(phoneNumber, null, message.getBody(), sentPI, deliveredPI);

然后在 Broadcast 中尝试捕获数据:

@Override
public void onReceive(Context context, Intent intent) {
    String message, prefix = "";
    String action = intent.getAction();
    long id = intent.getLongExtra(Constants.EXTRA_RAW_ID, -1L);  //here I receive id=-1

    // blah-blah.... 
}

我看到 Broadcast onReceive() 被调用 - 这意味着 Broadcast 以正确的方式注册,但额外内容仍然是空的。

有什么想法吗?

I'm trying to send via PendingIntent some extra data, like:

MyMessage message;
//...
Intent intent;
SmsManager sms = SmsManager.getDefault();
intent = new Intent(Constants.SENT_PLAIN);
intent.putExtra(Constants.EXTRA_RAW_ID, message.getId()); //putting long id (not -1L)
PendingIntent sentPI = PendingIntent.getBroadcast(activity, 0, intent, 0);
intent = new Intent(Constants.DELIVERED_PLAIN);
intent.putExtra(Constants.EXTRA_RAW_ID, message.getId());
PendingIntent deliveredPI = PendingIntent.getBroadcast(activity, 0, intent, 0);
sms.sendTextMessage(phoneNumber, null, message.getBody(), sentPI, deliveredPI);

Then in Broadcast trying to catch data:

@Override
public void onReceive(Context context, Intent intent) {
    String message, prefix = "";
    String action = intent.getAction();
    long id = intent.getLongExtra(Constants.EXTRA_RAW_ID, -1L);  //here I receive id=-1

    // blah-blah.... 
}

I see that Broadcast onReceive() called - which means that Broadcast registered in a proper way, but still extras are empty.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

愿得七秒忆 2024-12-02 14:20:16

将您正在待定意图中使用的数据作为附加内容放入意图中。
您将在 BroadCast 接收器的 onReceive 方法中获得此意图。
尝试如下定义待处理意图。

PendingIntent sentPI = PendingIntent.getBroadcast(activity, 0, intent,PendingIntent.FLAG_CANCEL_CURRENT);

Put data in intent you are using in pending intent as Extras.
You will get this intent in onReceive Method of BroadCast receiver.
Try to define Pending intent as below.

PendingIntent sentPI = PendingIntent.getBroadcast(activity, 0, intent,PendingIntent.FLAG_CANCEL_CURRENT);
后来的我们 2024-12-02 14:20:16

正如 待处理意图 中所述:

由于这种行为,为了检索 PendingIntent 的目的,了解何时将两个 Intent 视为相同非常重要。人们常犯的一个错误是创建多个 PendingIntent 对象,其 Intent 仅在“额外”内容上有所不同,并期望每次都能获得不同的 PendingIntent。这不会发生。 Intent 中用于匹配的部分与 Intent.filterEquals。如果您使用两个等效的 Intent 对象 Intent.filterEquals,那么您将为它们获得相同的 PendingIntent。

有两种典型的方法可以解决这个问题。

如果您确实需要多个不同的 PendingIntent 对象处于活动状态
同时(例如用作同时显示的两个通知)
同时),那么你需要确保有一些东西
他们的不同之处使他们与不同的人联系起来
待定意图。这可以是任何 Intent 属性
Intent.filterEquals,或提供给的不同请求代码整数
getActivity(Context, int, Intent, int), getActivities(Context, int, Intent\[\], int), getBroadcast(Context, int, Intent, int),或者
getService(Context, int, Intent, int)

如果您一次只需要激活一个 PendingIntent 来执行任一操作
您将使用的意图,然后您可以选择使用标志
FLAG_CANCEL_CURRENTFLAG_UPDATE_CURRENT 取消或修改
无论当前的 PendingIntent 与您的 Intent 相关联
供应。

as said on pending intent :

Because of this behavior, it is important to know when two Intents are considered to be the same for purposes of retrieving a PendingIntent. A common mistake people make is to create multiple PendingIntent objects with Intents that only vary in their "extra" contents, expecting to get a different PendingIntent each time. This does not happen. The parts of the Intent that are used for matching are the same ones defined by Intent.filterEquals. If you use two Intent objects that are equivalent as per Intent.filterEquals, then you will get the same PendingIntent for both of them.

There are two typical ways to deal with this.

If you truly need multiple distinct PendingIntent objects active at
the same time (such as to use as two notifications that are both shown
at the same time), then you will need to ensure there is something
that is different about them to associate them with different
PendingIntents. This may be any of the Intent attributes considered by
Intent.filterEquals, or different request code integers supplied to
getActivity(Context, int, Intent, int), getActivities(Context, int, Intent\[\], int), getBroadcast(Context, int, Intent, int), or
getService(Context, int, Intent, int).

If you only need one PendingIntent active at a time for any of the
Intents you will use, then you can alternatively use the flags
FLAG_CANCEL_CURRENT or FLAG_UPDATE_CURRENT to either cancel or modify
whatever current PendingIntent is associated with the Intent you are
supplying.

半寸时光 2024-12-02 14:20:16

任何在广播接收器中遇到 null extras 的人:

您应该使用
FLAG_MUTABLE

这是一个示例:

PendingIntent.getBroadcast(
     context,
     0,
     intent,
     PendingIntent.FLAG_MUTABLE //not FLAG_IMMUTABLE
)

Anyone who are experiencing getting null extras in Broadcast Receiver:

You should use
FLAG_MUTABLE

Here is an example:

PendingIntent.getBroadcast(
     context,
     0,
     intent,
     PendingIntent.FLAG_MUTABLE //not FLAG_IMMUTABLE
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文