Reputation: 188
I have a widget I am working on that controls a media player service. I have 4 buttons on my widget:
The four buttons are assigned a pending intent which sends a Broadcast to the player service with the required action contained in it as an extra.
The problem I am having is that when I assign the pending intents to the buttons as below, they all seem to have the same broadcast value. For example, with the code below all 4 buttons seem to send Globals.PlaybackActions.SKIP_FORWARD. However, if I comment out the code that assigns the pending intents except for the "Play" button then pressing "Play" broadcasts the correct value.
Is there something I'm not aware of with using multiple pending intents as broadcasts on a widget or have I made a silly mistake in my code that I cannot spot? Or secret option C?
I'm hopelessly lost and confused and would be very grateful for any help. Thanks.
public class PlayerWidget extends AppWidgetProvider {
private boolean serviceIsPlaying = false;
private PendingIntent pendingIntentPrev = null;
private PendingIntent pendingIntentNext = null;
private PendingIntent pendingIntentStop = null;
private PendingIntent pendingIntentPlay = null;
public void onReceive(Context context, Intent intent) {
if (!intent.getAction().equals(Globals.BROADCAST_WIDGET_UPDATE)) {
return;
}
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
ComponentName thisAppWidget = new ComponentName(context.getPackageName(), PlayerWidget.class.getName());
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget);
this.updateViews(context, appWidgetManager, appWidgetIds);
}
private void updateViews(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_player);
this.setClickIntents(context);
if (this.serviceIsPlaying) {
remoteViews.setImageViewResource(R.id.btnPlay, R.drawable.ic_action_pause);
}
else {
remoteViews.setImageViewResource(R.id.btnPlay, R.drawable.ic_action_play);
}
remoteViews.setTextViewText(R.id.txtArtistName, currentTrack.getArtistName());
remoteViews.setTextViewText(R.id.txtSongName, currentTrack.getSongTitle());
remoteViews.setTextViewText(R.id.txtAlbumName, currentTrack.getAlbumName());
remoteViews.setImageViewBitmap(R.id.imgAlbumArt, BitmapFactory.decodeFile(currentTrack.getAlbumArtPath()));
remoteViews.setOnClickPendingIntent(R.id.btnPrev, pendingIntentPrev);
remoteViews.setOnClickPendingIntent(R.id.btnNext, pendingIntentNext);
remoteViews.setOnClickPendingIntent(R.id.btnStop, pendingIntentStop);
remoteViews.setOnClickPendingIntent(R.id.btnPlay, pendingIntentPlay);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
private void setClickIntents(Context context) {
Intent intentPrev = new Intent(Globals.BROADCAST_PLAYBACK_ACTION);
Intent intentNext = new Intent(Globals.BROADCAST_PLAYBACK_ACTION);
Intent intentStop = new Intent(Globals.BROADCAST_PLAYBACK_ACTION);
Intent intentPlay = new Intent(Globals.BROADCAST_PLAYBACK_ACTION);
intentPrev.putExtra(Globals.EXTRA_PLAYBACK_ACTION, Globals.PlaybackActions.SKIP_BACKWARD);
intentNext.putExtra(Globals.EXTRA_PLAYBACK_ACTION, Globals.PlaybackActions.SKIP_FORWARD);
intentStop.putExtra(Globals.EXTRA_PLAYBACK_ACTION, Globals.PlaybackActions.STOP);
if (this.serviceIsPlaying) {
intentPlay.putExtra(Globals.EXTRA_PLAYBACK_ACTION, Globals.PlaybackActions.PAUSE);
}
else {
intentPlay.putExtra(Globals.EXTRA_PLAYBACK_ACTION, Globals.PlaybackActions.PLAY);
}
pendingIntentPrev = PendingIntent.getBroadcast(context, 0, intentPrev, 0);
pendingIntentNext = PendingIntent.getBroadcast(context, 0, intentNext, 0);
pendingIntentStop = PendingIntent.getBroadcast(context, 0, intentStop, 0);
pendingIntentPlay = PendingIntent.getBroadcast(context, 0, intentPlay, 0);
}
}
Upvotes: 2
Views: 252
Reputation: 121
I think the problem is a second parameter in PendingIntent.getBroadcast() method. The second parameter is requestCode and it should be different for all 4 PendingIntent-s you are using.For example:
pendingIntentPrev = PendingIntent.getBroadcast(context, 0, intentPrev, 0);
pendingIntentNext = PendingIntent.getBroadcast(context, 1, intentNext, 0);
pendingIntentStop = PendingIntent.getBroadcast(context, 2, intentStop, 0);
pendingIntentPlay = PendingIntent.getBroadcast(context, 3, intentPlay, 0);
I have the same problem few days ago and this helped me. Hope it will help you too.
Upvotes: 2