你好伙计我对小工具和待定意图相当新我在点击按钮时会遇到奇怪的行为 我的小部件上有3个按钮 播放/暂停
下一步
上
播放的广播工作正常,但是当我点击下一个按钮它正在执行上一个按钮应该做的事情而前一个按钮根本不做任何事情
这是代码
Intent playIntent = new Intent(BROADCAST_PLAYPAUSE);
PendingIntent playPendingIntent = PendingIntent.getBroadcast(
context, REQUEST_CODE, playIntent, PendingIntent.FLAG_UPDATE_CURRENT);
controlButtons.setOnClickPendingIntent(
R.id.bPlay, playPendingIntent);
Intent nextIntent = new Intent(BROADCAST_SWAP);
nextIntent.putExtra("nextprev", 1);
PendingIntent nextPendingIntent = PendingIntent.getBroadcast(
context, REQUEST_CODE, nextIntent, PendingIntent.FLAG_UPDATE_CURRENT);
controlButtons.setOnClickPendingIntent(
R.id.bNext, nextPendingIntent);
Intent previousIntent = new Intent(BROADCAST_SWAP);
previousIntent.putExtra("nextprev", -1);
PendingIntent previousPendingIntent = PendingIntent.getBroadcast(
context, REQUEST_CODE, previousIntent, PendingIntent.FLAG_UPDATE_CURRENT);
controlButtons.setOnClickPendingIntent(
R.id.bPrevious, previousPendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, controlButtons);
顺便说一句,这是我的应用程序的外观
答案 0 :(得分:0)
您为NEXT按钮创建PendingIntent
,如下所示:
Intent nextIntent = new Intent(BROADCAST_SWAP);
nextIntent.putExtra("nextprev", 1);
PendingIntent nextPendingIntent = PendingIntent.getBroadcast(
context, REQUEST_CODE, nextIntent, PendingIntent.FLAG_UPDATE_CURRENT);
然后为PREV按钮创建PendingIntent
,如下所示:
Intent previousIntent = new Intent(BROADCAST_SWAP);
previousIntent.putExtra("nextprev", -1);
PendingIntent previousPendingIntent = PendingIntent.getBroadcast(
context, REQUEST_CODE, previousIntent, PendingIntent.FLAG_UPDATE_CURRENT);
由于PREV的PendingIntent
具有完全相同的操作(BROADCAST_SWAP)和requestCode(REQUEST_CODE),因此它们被认为是匹配的,因此它们指向相同的PendingIntent
个对象。由于您在为PREV按钮调用PendingIntent.FLAG_UPDATE_CURRENT
时指定了getBroadcast()
,因此将PendingIntent
中的extras参数替换为PREV按钮的参数。这意味着PREV和NEXT按钮都将像PREV按钮一样。
要解决此问题,您需要确保NEXT和PREV的PendingIntent
是唯一的。您可以通过提供不同的requestCode
或不同的Intent操作来完成此操作。只是让额外的东西不同并不会让它们与众不同!