gpt4 book ai didi

小部件和多个未决 Intent 的 Android 问题

转载 作者:行者123 更新时间:2023-11-29 00:16:24 25 4
gpt4 key购买 nike

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

  • 上一个
  • 播放/暂停
  • 停止
  • 转发

这四个按钮被分配了一个挂起的 Intent ,它向播放器服务发送一个广播,其中包含作为额外内容的所需操作。

我遇到的问题是,当我将待定 Intent 分配给如下按钮时,它们似乎都具有相同的广播值。例如,在下面的代码中,所有 4 个按钮似乎都发送 Globals.PlaybackActions.SKIP_FORWARD。但是,如果我注释掉分配“播放”按钮以外的未决 Intent 的代码,则按下“播放”会广播正确的值。

在使用多个未决 Intent 作为小部件上的广播时,是否有我没有意识到的事情,或者我是否在我的代码中犯了一个我无法发现的愚蠢错误?还是 secret 选项 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 个 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);

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

关于小部件和多个未决 Intent 的 Android 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26614193/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com