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

Broadcast not received from Chrome Custom Tab menu item click

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

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);
}

然后我有一个 ActionBroadCastReceiver class 扩展 BroadcastReceiver:

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

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

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

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

像这样在清单文件中注册它:

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

并像这样设置意图:

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