android - 未收到来自 Chrome 自定义选项卡菜单项的广播点击

标签 android broadcastreceiver android-broadcast chrome-custom-tabs

我在 fragment 中执行以下操作(为方便起见进行了压缩):

intentBuilder = new CustomTabsIntent.Builder();
String label = "Test";
PendingIntent pendingIntent = createPendingIntent(ActionBroadcastReceiver.ACTION_TEST);
intentBuilder.addMenuItem(label, pendingIntent);

CustomTabActivityHelper.openCustomTab(
                    getActivity(), intentBuilder.build(), mUri, null);


private PendingIntent createPendingIntent(int actionSourceId) {
    Intent actionIntent = new Intent(getActivity().getApplicationContext(),
            ActionBroadcastReceiver.class);
    actionIntent.putExtra(ActionBroadcastReceiver.KEY_TEST, actionSourceId);

    return PendingIntent.getBroadcast(
            getActivity().getApplicationContext(), actionSourceId, actionIntent, 0);
}

然后我有一个扩展 BroadcastReceiverActionBroadCastReceiver 类:

@Override
public void onReceive(Context context, Intent intent) {
    Log.d(ActionBroadcastReceiver.class.getSimpleName(), "Broadcast Received");

Toast.makeText(context, "Received", Toast.LENGTH_SHORT).show();
    }
}

我的日志调用没有出现,点击菜单项时 toast 消息也没有出现,这让我相信广播从未发送或接收过。

最佳答案

尝试在您的广播中添加一个 Action 。

像这样在 list 文件中注册它:

<receiver android:name="com.example.app.MyReceiver" >
    <intent-filter>
        <action android:name="com.example.app.SOME_ACTION" />
    </intent-filter>
</receiver>

像这样设置 Intent :

Intent actionIntent = new Intent("com.example.app.SOME_ACTION");

关于android - 未收到来自 Chrome 自定义选项卡菜单项的广播点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35853205/

相关文章:

java - Android - 无法在我的 Activity 中从服务接收本地广播

android - 是否可以仅发布动态功能模块的更新?

android - 在 Android 上接听电话时获取 DTMF 信号

android - 是否可以从对话框中调用 onReceive 方法?

android - 如何监控 Android 设备中应用程序的后台服务?

android - 如何以编程方式从 Android 检索未读消息

android - Android Studio-Gradle-根据每个Android Studio安装而不是每个项目设置代理

java - onDrawerOpened 抽屉导航并在抽屉导航上设置 textView

Android ListView选择动画

Android - 我可以将 Wifi 状态更改的 Intent 发送给服务吗?