public class MyWidget extends AppWidgetProvider {
private static final String ACTION_UPDATE_CLICK =
"com.example.myapp.action.UPDATE_CLICK";
private static int mCount = 0;
private static String getMessage() {
return String.valueOf(mCount++);
}
private PendingIntent getPendingSelfIntent(Context context, String action) {
// An explicit intent directed at the current class (the "self").
Intent intent = new Intent(context, getClass());
intent.setAction(action);
return PendingIntent.getBroadcast(context, 0, intent, 0);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
String message = getMessage();
// Loop for every App Widget instance that belongs to this provider.
// Noting, that is, a user might have multiple instances of the same
// widget on
// their home screen.
for (int appWidgetID : appWidgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.my_widget);
remoteViews.setTextViewText(R.id.textView_output, message);
remoteViews.setOnClickPendingIntent(R.id.button_update,
getPendingSelfIntent(context,
ACTION_UPDATE_CLICK)
);
appWidgetManager.updateAppWidget(appWidgetID, remoteViews);
}
}
/**
* A general technique for calling the onUpdate method,
* requiring only the context parameter.
*
* @author John Bentley, based on Android-er code.
* @see <a href="http://android-er.blogspot.com
* .au/2010/10/update-widget-in-onreceive-method.html">
* Android-er > 2010-10-19 > Update Widget in onReceive() method</a>
*/
private void onUpdate(Context context) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance
(context);
// Uses getClass().getName() rather than MyWidget.class.getName() for
// portability into any App Widget Provider Class
ComponentName thisAppWidgetComponentName =
new ComponentName(context.getPackageName(),getClass().getName()
);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(
thisAppWidgetComponentName);
onUpdate(context, appWidgetManager, appWidgetIds);
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (ACTION_UPDATE_CLICK.equals(intent.getAction())) {
onUpdate(context);
}
}
}
4 回答
以下是一个应该有用的示例:
我发现了如何做到这一点 .
在>
<receiver><intent-filter>
标记中的AndroidManifest.xml
文件中添加操作:在提供程序中添加与操作名称匹配的常量:
在
onUpdate()
方法中添加与操作匹配的待处理意图:最后,在onRecieve()方法中,检查操作名称:
也喜欢网址:
How to correctly handle click events on Widget
如果您以不同的方式解决它,请提供此答案
这是另一个答案,具有以下好处:
它处理所有App Widget实例(用户可能在屏幕上有各种配置/大小的窗口小部件的多个实例) . 所有实例的编码都是官方文档规定的内容 . 请参阅Guide > App Widgets > Using the AppWidgetProvider Class,向下滚动到"ExampleAppWidgetProvider"的代码示例 .
onReceive
中的主力代码实际上调用了onUpdate
(因此减少了代码重复) .onUpdate(Context context)
中的代码是通用的,因此可以将其放入任何AppWidgetProvider子类中 .代码:
小部件看起来像这样
这 Build 在@Kels,@ SharonHaimPour和@ Erti-ChrisEelmaa的
getPendingSelfIntent
作品之上 .它还 Build 在Android-er > 2010-10-19 > Update Widget in onReceive() method(不是我)的基础上,它演示了如何在App Widget实例的基础上从onReceive调用onUpdate . 我将该代码设为通用并将其包装在
callOnUpdate
中 .