1

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

然后我有一个ActionBroadCastReceiver扩展类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 消息都没有出现,这让我相信广播永远不会发送或接收。

4

1 回答 1

3

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

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

<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");
于 2016-03-07T20:37:12.587 回答