待定意图广播未通过

时间:2018-03-09 10:52:53

标签: java android

我正在尝试在点击通知上的暂停按钮时发送广播。通知是从控制音乐播放器的后台服务触发的。

这是通知。

private void showPlayerNotification() {
    Intent notifyIntent = new Intent(this, PlayerActivity.class);
    notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    Intent pauseIntent = new Intent(Constants.RADIO_SERVICE_ACTION_RECEIVER);
    pauseIntent.putExtra(Constants.RADIO_SERVICE_ACTION, RadioService.PAUSE_ACTION);
    PendingIntent pausePendingIntent = PendingIntent.getBroadcast(this, 1, pauseIntent, PendingIntent.FLAG_CANCEL_CURRENT);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
            .setContentIntent(pendingIntent)
            .setStyle(new MediaStyle().setShowActionsInCompactView(0))
            .setSmallIcon(R.drawable.play)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
            .setColorized(true)
            .setColor(getResources().getColor(R.color.player_background))
            .setAutoCancel(true)
            .setContentTitle(mediaStream.getStreamName())
            .addAction(R.drawable.notification_pause, "Pause", pausePendingIntent);

    Notification mediaNotification = notificationBuilder.build();
    startForeground(1, mediaNotification);
}

广播接收器在服务中定义,并使用意图过滤器Constants.RADIO_SERVICE_ACTION_RECEIVER进行注册。我成功地使用相同的广播接收器来控制广播来自活动的播放。

上述pauseIntent广播没有通过广播接收器。可能是导致问题的服务上下文?

  

我已经使用PendingIntent.getActivity成功收到了我的播放器活动中的意图,但我不想在活动中处理它,因为这可能会弄乱活动代码。

0 个答案:

没有答案