我有这个简单的小部件,当我单击它时,它应该打开我的活动,它可以工作,但是在重新启动后不起作用。我必须先删除窗口小部件,然后再将其添加到主屏幕,因为单击窗口小部件时,窗口小部件不会响应且无法打开我的活动。那么问题出在哪里呢?
代码:
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
for(int i=0; i<N; i++){
int appWidgetId = appWidgetIds[i];
context.startService(new Intent(context, WidgetService.class));
Intent intent = new Intent(context, WidgetDialog.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
views.setOnClickPendingIntent(R.id.layout_widget, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
@Override
public void onEnabled(Context context){
context.startService(new Intent(context, WidgetService.class));
}
@Override
public void onDisabled(Context context){
context.stopService(new Intent(context, WidgetService.class));
}
重新启动后,将调用OnEnabled而不是onUpdate,因此将代码从onUpdate移到单独的方法中,然后调用该方法以从onEnabled更新所有小部件
小部件继承的AppWidgetProvider在启动时从操作系统接收到onUpdate
时将调用ACTION_APPWIDGET_UPDATE
。
如果您在不调用onReceive
的情况下覆盖了super.onReceive()
,则需要自己拦截此Intent。