-1

J'ai un bug étrange.Notification Android: bug lors de l'utilisation de plusieurs setOnClickPendingIntent

J'ai reçu une notification avec 3 boutons (retour, suivant, lecture/pause). Je veux définir un différent pendingIntent sur chacun de ces trois boutons qui lancent un service. Voici mon code:

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); 

Puis, dans mon service, je reçois l'intention et la valeur de « do_action » définie comme un supplément:

@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" !!! 
    }  
} 

Le bug est ici ... J'ai toujours obtenir "précédent" quand j'obtiens la valeur "do_action" (pas seulement quand je clique sur le bouton précédent, aussi quand je clique sur le bouton play ou sur les boutons suivants).

Quelqu'un peut-il m'aider à résoudre ce problème?

Merci!

Répondre

2

Vous avez utilisé le même code de requête 0 pour l'ensemble de vos Pendingintent. Vous devez utiliser différents REQUEST_CODE pour différentes PendingIntent:

PendingIntent.getService(MyApp.mainActivity, REQUEST_CODE, Intent, PendingIntent.FLAG_UPDATE_CURRENT) 

Essayez ceci:

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); 

Espérons que cela aidera ~

+0

Excellent !!! Merci !!! –

+0

Heureux de vous aider :) – FAT