Android 通知:使用多个 setOnClickPendingIntent 时的错误

Reg*_*_AG 0 java android android-intent android-notifications android-pendingintent

我有一个奇怪的错误。

我收到了一个带有 3 个按钮(后退、下一步、播放/暂停)的通知。我想在启动服务的这三个按钮中的每一个上设置不同的 pendingIntent。这是我的代码:

    Intent playPauseIntent = new Intent(MyApp.mainActivity, NotificationService.class);
    playPauseIntent.putExtra("do_action", "play");
    contentView.setOnClickPendingIntent(R.id.notifPlayPauseImageView, PendingIntent.getService(MyApp.mainActivity, 0, playPauseIntent, PendingIntent.FLAG_UPDATE_CURRENT));            

    Intent nextRadioIntent = new Intent(MyApp.mainActivity, NotificationService.class);
    nextRadioIntent.putExtra("do_action", "next");
    contentView.setOnClickPendingIntent(R.id.notifNextImageView, PendingIntent.getService(MyApp.mainActivity, 0, nextRadioIntent, PendingIntent.FLAG_UPDATE_CURRENT));

    Intent previousRadioIntent = new Intent(MyApp.mainActivity, NotificationService.class);
    previousRadioIntent.putExtra("do_action", "previous");
    contentView.setOnClickPendingIntent(R.id.notifPreviousImageView, PendingIntent.getService(MyApp.mainActivity, 0, previousRadioIntent, PendingIntent.FLAG_UPDATE_CURRENT));

    notification.contentView = contentView;
    mNotificationManager.notify(1, notification); 
Run Code Online (Sandbox Code Playgroud)

然后,在我的服务中,我将“do_action”的意图和值定义为额外的:

@Override
public int onStartCommand(Intent intent,  int flags, int startId) { 

   if(intent != null && intent.getExtras() != null)
   {
      String action = (String) intent.getExtras().get("do_action"); // BUG HERE ! I always get "previous" !!!
   }       
}
Run Code Online (Sandbox Code Playgroud)

错误就在这里......当我获得“do_action”值时,我总是得到“上一个”(不仅当我点击上一个按钮时,也当我点击播放或下一个按钮时)。

任何人都可以帮我解决这个问题吗?

谢谢 !

Fer*_*med 5

0对所有Pendingintent. 您应该REQUEST_CODE对不同的使用不同的PendingIntent

PendingIntent.getService(MyApp.mainActivity, REQUEST_CODE, Intent, PendingIntent.FLAG_UPDATE_CURRENT)
Run Code Online (Sandbox Code Playgroud)

尝试这个:

Intent playPauseIntent = new Intent(MyApp.mainActivity, NotificationService.class);
playPauseIntent.putExtra("do_action", "play");
contentView.setOnClickPendingIntent(R.id.notifPlayPauseImageView, PendingIntent.getService(MyApp.mainActivity, 0, playPauseIntent, PendingIntent.FLAG_UPDATE_CURRENT));            

Intent nextRadioIntent = new Intent(MyApp.mainActivity, NotificationService.class);
nextRadioIntent.putExtra("do_action", "next");
contentView.setOnClickPendingIntent(R.id.notifNextImageView, PendingIntent.getService(MyApp.mainActivity, 1, nextRadioIntent, PendingIntent.FLAG_UPDATE_CURRENT));

Intent previousRadioIntent = new Intent(MyApp.mainActivity, NotificationService.class);
previousRadioIntent.putExtra("do_action", "previous");
contentView.setOnClickPendingIntent(R.id.notifPreviousImageView, PendingIntent.getService(MyApp.mainActivity, 2, previousRadioIntent, PendingIntent.FLAG_UPDATE_CURRENT));

notification.contentView = contentView;
mNotificationManager.notify(1, notification); 
Run Code Online (Sandbox Code Playgroud)

希望这会有所帮助~