public class ExampleAppWidgetProvider extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
final int N = appWidgetIds.length;
// Perform this loop procedure for each App Widget that belongs to this
// provider
for (int i = 0; i < N; i++) {
int appWidgetId = appWidgetIds[i];
// Get the layout for the App Widget and attach an on-click listener
// to the button
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.example_appwidget);
Intent intent = new Intent(context, Fragment_testActivity.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.button1, pendingIntent);
// Tell the AppWidgetManager to perform an update on the current app
// widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
}
当小部件添加到主屏幕并单击button1时,为什么button1不会触发onUpdate
(也不是onReceive
)?添加/删除小部件会触发这些事件,但不会单击按钮。
答案 0 :(得分:0)
我首先看到的是:
new Intent(context, Fragment_testActivity.class);
您将活动指定为接收组件。该活动与更新过程无关。尝试将ExampleAppWidgetProvider设置为intent的组件,或者根本不指定组件(例如,使用new Intent()
)。