小工具和多个挂起意图的Android问题



我正在开发一个小部件,用于控制媒体播放器服务。我的小工具上有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);
        }
    }

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

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

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

最新更新