Android小部件-使用带有RemoteView的SetOnClickPendingEvent未收到意图
我目前正在使用三个Android小部件-使用带有RemoteView的SetOnClickPendingEvent未收到意图,android,android-intent,android-widget,android-pendingintent,Android,Android Intent,Android Widget,Android Pendingintent,我目前正在使用三个图像按钮创建我的第一个小部件。 我已经非常仔细地遵循了所描述的答案,但是我没能让它起作用 我定义了三个字符串,它们是我的操作: private String playAction = "playService"; private String stopAction = "stopService"; private String resetAction = "resetService"; 接下来在我的OnUpdate()函数中添加setonClickPendingContent
图像按钮创建我的第一个小部件。
我已经非常仔细地遵循了所描述的答案,但是我没能让它起作用
我定义了三个字符串,它们是我的操作:
private String playAction = "playService";
private String stopAction = "stopService";
private String resetAction = "resetService";
接下来在我的OnUpdate()函数中添加setonClickPendingContent
:
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
remoteViews.setOnClickPendingIntent(R.id.widget_play, getPendingSelfIntent(context, playAction));
remoteViews.setOnClickPendingIntent(R.id.widget_pause, getPendingSelfIntent(context, stopAction));
remoteViews.setOnClickPendingIntent(R.id.widget_reset, getPendingSelfIntent(context, resetAction));
manager = appWidgetManager;
// Build the intent to call the service
Intent intent = new Intent(context.getApplicationContext(), UpdateWidgetService.class);
// Update the widgets via the service
context.startService(intent);
}
getPendingSelfIntent
函数定义如下:
protected PendingIntent getPendingSelfIntent(Context context, String action) {
Intent intent = new Intent(context, MyWidgetProvider.class);
intent.setAction(action);
return PendingIntent.getBroadcast(context, 0, intent, 0);
}
但是,onReceive
功能不接收任何上述定义的操作。我目前记录所有传入意图的操作,但唯一通过的是android.appwidget.action.appwidget\u UPDATE
action
为了完成,我在清单中定义了以下操作:
<receiver
android:name="MyWidgetProvider"
android:icon="@drawable/ic_launcher"
android:label="My Widget">
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_info" />
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="playService" />
<action android:name="stopService" />
<action android:name="resetService" />
</intent-filter>
</receiver>
在这件事上的任何帮助都是感激的,因为我完全没有主意了。我已经搜索过类似的问题,但它们都描述了我已经尝试过的问题。我认为您在设置了PendingEvents后错过了小部件的更新:
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
如果这没有帮助,您也应该发布UpdateWidgetService.class
顺便说一句,UpdateWidgetService.class做什么?我认为在设置PendingEvents后,您缺少对小部件的更新:
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
如果这没有帮助,您也应该发布UpdateWidgetService.class
顺便问一下,UpdateWidgetService.class做什么?发布你的UpdateWidgetService类我认为你应该使用PendingContent.FLAG\u UPDATE\u CURRENT
作为PendingContent.getBroadcast(…)
的最后一个参数。尝试替换return pendingent.getBroadcast(context,0,intent,0)代码>使用返回pendingent.getBroadcast(上下文,0,意图,pendingent.FLAG\u UPDATE\u CURRENT)
.Post您的UpdateWidgetService classI我认为您应该使用PendingEvent.FLAG\u UPDATE\u CURRENT
作为PendingEvent.getBroadcast(…)
的最后一个参数。尝试替换return pendingent.getBroadcast(context,0,intent,0)代码>使用返回pendingent.getBroadcast(上下文,0,意图,pendingent.FLAG\u UPDATE\u CURRENT)
。这个答案是正确的,需要调用updateAppWidget
。我已经创建了一个有工作文件的。此外,不需要在清单文件中添加自定义操作,如中所指定。如果OP可以接受此答案,则可以将此注释内容附加到答案中。您能否解释此函数的内部功能?文件并没有向我提供全部情况。我现在就试试你的建议。这个函数对小部件本身进行更新。您准备RemoteView,并使用此功能将其传递给具有给定id的小部件。您可以使用相同的内容更新所有小部件,或使用不同的内容更新每个小部件,这取决于您是传递数组中的所有id还是一个id。您可以在此处查看文档:,这里的源代码是:我可以在5小时内给你赏金,但你可以放心。这个答案是正确的,updateAppWidget
需要调用。我已经创建了一个有工作文件的。此外,不需要在清单文件中添加自定义操作,如中所指定。如果OP可以接受此答案,则可以将此注释内容附加到答案中。您能否解释此函数的内部功能?文件并没有向我提供全部情况。我现在就试试你的建议。这个函数对小部件本身进行更新。您准备RemoteView,并使用此功能将其传递给具有给定id的小部件。您可以使用相同的内容更新所有小部件,或使用不同的内容更新每个小部件,这取决于您是传递数组中的所有id还是一个id。您可以在此处查看文档:,这里的源代码是:我可以在5小时内给你赏金,但你可以放心。