问题
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);
}
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.
回答1:
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>
And setup intent like this:
Intent actionIntent = new Intent("com.example.app.SOME_ACTION");
来源:https://stackoverflow.com/questions/35853205/broadcast-not-received-from-chrome-custom-tab-menu-item-click