你能在本地广播中使用待定意图吗?

21

我对使用本地广播的挂起意图很感兴趣。为了让自己清楚,我正在使用以下内容注册接收器并发送广播:android.support.v4.content.LocalBroadcastManager

我在服务中有一个本地广播接收器,它可以正常工作。我正在尝试从自定义通知布局发送本地广播,其中包括可单击的项。

该本地广播接收器仅接收简单的动作意图。我曾尝试过类似这样的东西,但没有成功:

Intent backintent = new Intent("GOTO_START_BROADCAST");
PendingIntent backIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, backintent, 0);
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);
contentView.setOnClickPendingIntent(R.id.imageView1, backIntent);

在这种情况下,使用IntentService而不是LocalBroadcast有什么问题吗? - IgorGanapolsky
1个回答

29

我有意使用带有本地广播的挂起意图。

这是不可能的。

PendingIntent 的目的是允许其他进程执行您请求的操作,例如发送广播。

LocalBroadcastManager 的目的在于将广播限制在您的进程内部

因此,PendingIntent 可以发出常规广播,但不能通过LocalBroadcastManager 发送广播。


有没有办法让我使用LocalBroadcastManager来实现类似于PendingIntent.UPDATE_CURRENT等的功能?基本上,当有很多事件同时发生时,我想只发送一个LocalBroadcast而不是每个事件都发送一个。如何使用LocalBroadcast来实现这一点? - 500865
3
PendingIntent 无法与 LocalBroadcastManager 配合使用。 PendingIntent 被设计用于跨进程边界工作;而 LocalBroadcastManager 被设计为 不要 跨进程边界工作。 - CommonsWare
这就是为什么我的“BroadcastReceiver”没有接收广播的原因。通过使用传统的“BroadcastManager”解决了问题。 - AxeEffect

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接