0

hello guys i am fairly new to widgets and pending intent i am getting a weird behavior while clicking buttons I got 3 buttons on my widget Play /Pause

Next

Previous

the broadcast for Play is working fine but when i click on the next button It is doing what previous button is supposed to do while previous button doesn't do anything at all

I made sure that id's weren't swapped.

here is the code

Intent playIntent = new Intent(BROADCAST_PLAYPAUSE);

        PendingIntent playPendingIntent = PendingIntent.getBroadcast(
                context, REQUEST_CODE, playIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        controlButtons.setOnClickPendingIntent(
                R.id.bPlay, playPendingIntent);

        Intent nextIntent     = new Intent(BROADCAST_SWAP);
        nextIntent.putExtra("nextprev", 1);

        PendingIntent nextPendingIntent = PendingIntent.getBroadcast(
                context, REQUEST_CODE, nextIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        controlButtons.setOnClickPendingIntent(
                R.id.bNext, nextPendingIntent);



        Intent previousIntent = new Intent(BROADCAST_SWAP);
        previousIntent.putExtra("nextprev", -1);
        PendingIntent previousPendingIntent = PendingIntent.getBroadcast(
                context, REQUEST_CODE, previousIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        controlButtons.setOnClickPendingIntent(
                R.id.bPrevious, previousPendingIntent);
              appWidgetManager.updateAppWidget(appWidgetIds, controlButtons);         

EDIT :I put the code of next button after the previous button and now nothing works...only play button works..has it something to do with their intents using the same string??and is this an error of the flags of the intent ??

by the way here is how my app looks

enter image description here

Ankit Srivastava
  • 853
  • 1
  • 9
  • 28

1 Answers1

0

You create the PendingIntent for NEXT button like this:

   Intent nextIntent     = new Intent(BROADCAST_SWAP);
   nextIntent.putExtra("nextprev", 1);
   PendingIntent nextPendingIntent = PendingIntent.getBroadcast(
            context, REQUEST_CODE, nextIntent, PendingIntent.FLAG_UPDATE_CURRENT);

Then you create the PendingIntent for PREV button like this:

   Intent previousIntent = new Intent(BROADCAST_SWAP);
   previousIntent.putExtra("nextprev", -1);
   PendingIntent previousPendingIntent = PendingIntent.getBroadcast(
            context, REQUEST_CODE, previousIntent, PendingIntent.FLAG_UPDATE_CURRENT);

Since the PendingIntent for PREV has exactly the same action (BROADCAST_SWAP) and requestCode (REQUEST_CODE) they are considered matching, therefore they point to the same PendingIntent objects. Since you specify PendingIntent.FLAG_UPDATE_CURRENT when you call getBroadcast() for the PREV button, this replaces the extras parameters in the PendingIntent with the ones for PREV button. This means that both PREV and NEXT button will act like PREV button.

To fix this you need to make sure that the PendingIntent for NEXT and PREV are unique. You can do this by providing either different requestCode or a different Intent action. Just making the extras different does not make them different!

David Wasser
  • 93,459
  • 16
  • 209
  • 274