Widget和多个待定意图的Android问题

时间:2014-10-28 17:01:55

标签: android widget broadcast

我正在制作一个控制媒体播放器服务的小部件。我的小部件上有4个按钮:

  • 播放/暂停
  • 停止
  • 转发

这四个按钮被分配了一个待处理的意图,该意图将广播发送到播放器服务,其中包含所需的操作作为额外的。

我遇到的问题是,当我将挂起的意图分配给按钮时,它们似乎都具有相同的广播值。例如,使用下面的代码,所有4个按钮似乎都发送了Globals.PlaybackActions.SKIP_FORWARD。但是,如果我注释掉除“播放”按钮之外的指定待处理意图的代码,则按“播放”会广播正确的值。

在小部件上使用多个待处理的意图作为广播,或者我在代码中犯了一个我无法发现的愚蠢错误,是否有一些我不知道的事情?或秘密选项C?

我绝望地迷茫和困惑,非常感谢任何帮助。感谢。

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

1 个答案:

答案 0 :(得分:2)

我认为问题是PendingIntent.getBroadcast()方法中的第二个参数。第二个参数是requestCode,它对于您正在使用的所有4个PendingIntent-s应该是不同的。例如:

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

前几天我遇到了同样的问题,这对我很有帮助。希望它也能帮到你。