Broadcast not received from Chrome Custom Tab menu item click

為{幸葍}努か 提交于 2019-12-11 14:44:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!