我已经实现了一个 App Widget 以在单击时启动我的活动。
onUpdate()
方法WidgetProvider
:
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
final int N = appWidgetIds.length;
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.mywidgetprovider_layout);
// ....update updateViews here
appWidgetManager.updateAppWidget(appWidgetId, updateViews);
Intent onClickedIntent = new Intent(context,MyActivity.class);
PendingIntent pi = PendingIntent.getActivity(context, 0, onClickedIntent, 0);
updateViews.setOnClickPendingIntent(R.id.myView, pi);
appWidgetManager.updateAppWidget(appWidgetId, updateViews);
}
}
在主屏幕上添加小部件后,它按预期工作。
但有时之后,它无法再次启动活动!我必须删除小部件并再次添加。
我该如何解决?请帮忙。