gpt4 book ai didi

android - 如何在 PendingIntent 中发送有序广播?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:03:16 28 4
gpt4 key购买 nike

我想在 PendingIntent 中发送有序广播。但是我只找到了PendingIntent.getBroadcast(this, 0, intent, 0),我认为它只能发送常规广播。那么,我能做什么呢?

最佳答案

我从 http://justanapplication.wordpress.com/tag/pendingintent-getbroadcast 得到这个:

If the onFinished argument is not null then an ordered broadcast is performed.

所以你可能想尝试调用 PendingIntent.send设置了 onFinished 参数。

但是,我遇到了必须从通知发送 OrderedBroadcast 的问题。我通过创建一个仅将 Intent 作为 OrderedBroadcast 转发的 BroadcastReceiver 使其工作。我真的不知道这是否是一个好的解决方案。

所以我开始创建一个 Intent,它包含要转发到的操作的名称:

// the name of the action of our OrderedBroadcast forwarder
Intent intent = new Intent("com.youapp.FORWARD_AS_ORDERED_BROADCAST");
// the name of the action to send the OrderedBroadcast to
intent.putExtra(OrderedBroadcastForwarder.ACTION_NAME, "com.youapp.SOME_ACTION");
intent.putExtra("some_extra", "123");
// etc.

在我的例子中,我将 PendingIntent 传递给了通知:

PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
Notification notification = new NotificationCompat.Builder(context)
.setContentTitle("Notification title")
.setContentText("Notification content")
.setSmallIcon(R.drawable.notification_icon)
.setContentIntent(pendingIntent)
.build();
NotificationManager notificationManager = (NotificationManager)context
.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify((int)System.nanoTime(), notification);

然后我在 list 中定义了以下接收器:

<receiver
android:name="com.youapp.OrderedBroadcastForwarder"
android:exported="false">
<intent-filter>
<action android:name="com.youapp.FORWARD_AS_ORDERED_BROADCAST" />
</intent-filter>
</receiver>
<receiver
android:name="com.youapp.PushNotificationClickReceiver"
android:exported="false">
<intent-filter android:priority="1">
<action android:name="com.youapp.SOME_ACTION" />
</intent-filter>
</receiver>

然后 OrderedBroadcastForwarder 看起来如下:

public class OrderedBroadcastForwarder extends BroadcastReceiver
{
public static final String ACTION_NAME = "action";

@Override
public void onReceive(Context context, Intent intent)
{
Intent forwardIntent = new Intent(intent.getStringExtra(ACTION_NAME));
forwardIntent.putExtras(intent);
forwardIntent.removeExtra(ACTION_NAME);

context.sendOrderedBroadcast(forwardIntent, null);
}
}

关于android - 如何在 PendingIntent 中发送有序广播?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11025988/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com