未从Chrome“自定义标签"菜单项单击“接收广播"
问题描述:
我正在片段中进行以下操作(为方便起见,此内容已精简):
I am doing the following within a Fragment (condensed for convenience):
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
类:
Then I have an ActionBroadCastReceiver
class that extends BroadcastReceiver
:
@Override
public void onReceive(Context context, Intent intent) {
Log.d(ActionBroadcastReceiver.class.getSimpleName(), "Broadcast Received");
Toast.makeText(context, "Received", Toast.LENGTH_SHORT).show();
}
}
单击菜单项时,我的日志呼叫没有出现,并且吐司消息也没有出现,这使我相信广播从未发送或接收.
My log call does not appear and neither does the toast message when the menu item is clicked, which leads me to believe the broadcast is never sent nor received.
答
尝试在广播中添加操作.
Try adding an action to your broadcast.
将其注册到清单文件中,如下所示:
Register it in manifest file like this:
<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");