Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android小部件问题和多个未决意图_Android_Widget_Broadcast - Fatal编程技术网

Android小部件问题和多个未决意图

Android小部件问题和多个未决意图,android,widget,broadcast,Android,Widget,Broadcast,我有一个控制媒体播放器服务的小部件。我的小部件上有4个按钮: 先前的 播放/暂停 停止 前进 这四个按钮被分配了一个挂起的意图,该意图向播放器服务发送一个广播,其中包含一个额外的所需操作 我遇到的问题是,当我将挂起的意图分配给下面的按钮时,它们似乎都具有相同的广播值。例如,在下面的代码中,所有4个按钮似乎都向前发送Globals.PlaybackActions.SKIP_。但是,如果我注释掉除了“播放”按钮之外分配挂起意图的代码,则按“播放”广播正确的值 在小部件上使用多个挂起的意图作为广播

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

  • 先前的
  • 播放/暂停
  • 停止
  • 前进
这四个按钮被分配了一个挂起的意图,该意图向播放器服务发送一个广播,其中包含一个额外的所需操作

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

在小部件上使用多个挂起的意图作为广播是否有什么我不知道的地方,或者我在代码中犯了一个我无法发现的愚蠢错误?还是秘密方案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个PendingEvent-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);

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

太棒了-这解决了我的问题。我从网上的一篇文章中得到了代码的概念,它们都是零。不知道他们希望他们的工作如何。谢谢你,迈克,我真的很感激。