AppWidgetProvider.onRestored() 究竟是什么时候被调用的?

When exactly is AppWidgetProvider.onRestored() being called?

在什么情况下我应该覆盖 onRestored?

他们只是指清单中可以 enabled/disabled 的备份吗?当应用程序也更新时,是否有可能调用此方法?或者在 appWidgetIds 可能发生变化的任何其他情况下?我认为 ID 应该永远保持不变,但并非所有 manufacturers/launchers 都以相同的方式对待小部件...

这对 phantom/ghost 小部件有用吗?

/**
 * Called in response to the {@link AppWidgetManager#ACTION_APPWIDGET_RESTORED} broadcast
 * when instances of this AppWidget provider have been restored from backup.  If your
 * provider maintains any persistent data about its widget instances, override this method
 * to remap the old AppWidgetIds to the new values and update any other app state that may
 * be relevant.
 *
 * <p>This callback will be followed immediately by a call to {@link #onUpdate} so your
 * provider can immediately generate new RemoteViews suitable for its newly-restored set
 * of instances.
 *
 * {@more}
 *
 * @param context
 * @param oldWidgetIds
 * @param newWidgetIds
 */
public void onRestored(Context context, int[] oldWidgetIds, int[] newWidgetIds) {
}

onRestored()方法与Android的backup/restore过程密切相关。

在Android中,可以backup/restore应用数据到Google通过BackupManagerService驱动。此时备份数据恢复时,app widget数据也恢复,恢复完成后调用onRestored()

另外,AppWidgetIduid管理,以从0开始递增的形式看到,由于恢复时可以更改id,oldIdnewId 在调用 onRestored() 时传递。

这个过程可以在 AppWidgetServiceImpl.java 中找到,关于 Android 备份系统的概述,请参阅下面的 url。

https://developer.android.com/guide/topics/data/backup