Android - 奇怪的应用小部件行为:它有时会删除其内容本身

时间:2015-09-11 19:59:05

标签: android android-appwidget

我整个星期都在努力解决这个问题。

我有一个应用小部件,可以调用服务来更新自己。

有时,在我重新运行我的应用后,小部件会在后台正确更新;然而,当我按下后退按钮时,小部件内容消失,只留下默认的xml内容。

使用调试器,永远不会调用AppWidgetProvider onUpdate;有时这也会在一段时间后不使用手机而发生;或打开和关闭wifi。

它不显示“小部件错误”消息。这就像重新创建了小部件(内存问题?)但没有执行updateWidget方法。

一旦我从不同的进程ID读取更新小部件就会导致忽略更新,但我不知道如何测试它。

1 个答案:

答案 0 :(得分:0)

问题在于调用updateAllWidgets从default.xml重新创建窗口小部件,然后在那个窗口上更新远程视图。

如果只希望更新窗口小部件的某些部分,则应在已修改的窗口小部件上调用partiallyUpdateAppWidget

/** Update a given widget (id) with the given remoteViews instructions */
    protected static void updateWidget(Context context, RemoteViews remoteView, int widgetId){
        AppWidgetManager manager = AppWidgetManager.getInstance(context);
        manager.partiallyUpdateAppWidget(widgetId, remoteView);
    }