Android widget赢了';t多次切换布局
我正在将一个小部件添加到一个旧应用程序中,我正在从一个用于在后台轮询数据(在警报中)的服务更新它。每次服务得到结果时,我都会更新小部件。这目前工作正常Android widget赢了';t多次切换布局,android,android-widget,Android,Android Widget,我正在将一个小部件添加到一个旧应用程序中,我正在从一个用于在后台轮询数据(在警报中)的服务更新它。每次服务得到结果时,我都会更新小部件。这目前工作正常 // Called from inside my service when it has results private void updateWidget(List<Earthquake> earthquakes) { AppWidgetManager manager = AppWidgetManager.getInstan
// Called from inside my service when it has results
private void updateWidget(List<Earthquake> earthquakes) {
AppWidgetManager manager = AppWidgetManager.getInstance(this);
int[] appWidgetIds = manager.getAppWidgetIds(new ComponentName(this, WhatsShakingWidgetProvider.class));
if (appWidgetIds == null || appWidgetIds.length == 0)
return;
Earthquake earthquake = earthquakes.get(0);
RemoteViews views = new RemoteViews(getPackageName(), R.layout.widget_detail);
// Update views
views.setTextViewText(R.id.widget_detail_latest_magnitude, earthquake.getFormattedMagnitude());
// etc...
// Update each widget
for(int appWidgetId : appWidgetIds) {
manager.updateAppWidget(appWidgetId, views);
}
}
这是onUpdate
中的代码,应该更新小部件,但不是:
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean backgroundUpdatesEnabled = prefs.getBoolean(PreferenceActivity.KEY_PREF_ALLOW_BG_NOTIFICATIONS,
DefaultPrefs.BG_NOTIFICATIONS_ENABLED);
if (!backgroundUpdatesEnabled) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_error);
// Update click to take to preferences
Intent intent = new Intent(context, PreferenceActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.widget_error_parent_container, pendingIntent);
// Update each widget
appWidgetManager.updateAppWidget(appWidgetIds, views);
} else {
// Let's get some data for the user! Service does the work of updating the views.
WakefulIntentService.sendWakefulWork(context, GeonetService.class);
}
}
Logcat中未记录任何错误。通过此步骤,我在预期的情况下正确输入if
的每个部分(即,如果用户关闭了设置,那么我将创建remoteview视图
作为widget\u error
,否则我将启动服务)
为什么通过onUpdate
第一次正确显示窗口小部件\u错误
布局,而不是在用户启用然后禁用后台更新设置时正确显示?
我曾尝试将其包装在一个
RelativeLayout
中,并设置错误消息/内容的可见性,但表现出相同的行为-我无法在最初隐藏错误消息后将其显示出来。我最终在两个位置复制了代码(首选项活动和小部件提供程序)它成功了。唯一的变量似乎是上下文
对象
出于某种原因,您在AppWidgetProvider
(即在onUpdate
)中获得的上下文
实例仅在第一次工作时才工作,或者在我自己发送广播时不工作。我不知道为什么
我将复制的代码取出到一个单独的类中,只需传入我可用的上下文
实例,无论它是服务
、活动
,还是AppWidgetProvider
(它是广播接收器
)。这将正确地更新小部件,我可以在上下文中的任何位置调用它
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean backgroundUpdatesEnabled = prefs.getBoolean(PreferenceActivity.KEY_PREF_ALLOW_BG_NOTIFICATIONS,
DefaultPrefs.BG_NOTIFICATIONS_ENABLED);
if (!backgroundUpdatesEnabled) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_error);
// Update click to take to preferences
Intent intent = new Intent(context, PreferenceActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.widget_error_parent_container, pendingIntent);
// Update each widget
appWidgetManager.updateAppWidget(appWidgetIds, views);
} else {
// Let's get some data for the user! Service does the work of updating the views.
WakefulIntentService.sendWakefulWork(context, GeonetService.class);
}
}