【问题标题】:When exactly is AppWidgetProvider.onRestored() being called?究竟何时调用 AppWidgetProvider.onRestored()?
【发布时间】:2020-08-01 20:38:18
【问题描述】:

在哪些情况下我应该覆盖 onRestored?

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

这对幻像/幽灵小部件有用吗?

/**
 * 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) {
}

【问题讨论】:

    标签: java android android-appwidget appwidgetprovider


    【解决方案1】:

    onRestored() 方法与 Android 的备份/恢复过程密切相关。

    在 Android 中,可以通过BackupManagerService 将应用数据备份/恢复到 Google Drive。此时备份数据恢复时,app小部件数据也恢复,恢复完成时调用onRestored()

    另外,AppWidgetId是由uid管理的,看是从0开始递增的形式。由于恢复时id可以改变,所以oldIdnewIdonRestored() 被调用。

    这个过程可以在AppWidgetServiceImpl.java追踪,Android备份系统的概述,参考下面的url。

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

    【讨论】:

      猜你喜欢
      • 2015-08-11
      • 2014-01-16
      • 2014-09-09
      • 2023-02-25
      • 2016-04-14
      • 2015-10-12
      • 2013-03-18
      • 2012-02-13
      • 2015-10-18
      相关资源
      最近更新 更多