Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/181.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android widget赢了';t多次切换布局_Android_Android Widget - Fatal编程技术网

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);
    }
}