我在 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);
}
然后我有一个扩展 BroadcastReceiver
的 ActionBroadCastReceiver
类:
@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/