每当用户第一次安装我的应用程序时,都会随机生成一个安装 ID(比如安装 ID = 123)。我正在尝试备份这个 instalationID,这样当用户卸载然后安装我的应用程序时,旧的 instalationID (123) 将再次分配给他,而不是新的。毕竟,他是同一个用户。
我有一个名为 的共享首选项文件,SESSION_INFO_PREFERENCE_KEY
其中包含 instalationID,我尝试备份共享首选项文件。后备经理是:
public class ADCBackupAgent extends BackupAgentHelper {
// The name of the SharedPreferences file instalation ID
static final String INSTID = "SESSION_INFO_PREFERENCE_KEY"; //SESSION_INFO_INSTALLATION_UID
// A key to uniquely identify the set of backup data
static final String INSTID_BACKUP_KEY = "inst_id";
// Allocate a helper and add it to the backup agent
@Override
public void onCreate() {
Log.i("OnCreate Method","!!!!!!ON create called!!!!!!!!!!");
SharedPreferencesBackupHelper helper = new SharedPreferencesBackupHelper(this, INSTID);
addHelper(INSTID_BACKUP_KEY, helper);
}
}
此外,每当第一次生成安装 ID 时,我都会调用备份管理器,例如:
//this pointing to the current Activity
BackupManager backUpManager = new BackupManager(this);
backUpManager.dataChanged();
为了完整起见:
<application
android:icon="@drawable/application_icon"
android:label="@string/app_name"
android:name="MyAppName"
android:theme="@style/FragmentTheme"
android:allowBackup="true"
android:backupAgent=".ADCBackupAgent">
...
<meta-data android:name="com.google.android.backup.api_key" android:value="AEdPqr..." />
</application>
当我在本地传输(使用该bmgr
工具)上进行备份时,我的代码工作得非常好,但是,当我尝试在 Google 的云存储上备份数据时,我得到:
04-28 15:12:24.305: W/BackupTransportService(390): Not ready for backup request right now: [OperationScheduler: enabledState=true lastSuccess=2014-04-03/15:10:49 moratoriumSet=2014-04-28/14:43:18 moratorium=2014-04-29/14:43:18 trigger=1969-12-31/19:00:00]
此外,该onCreate()
方法永远不会被调用。
当我在本地传输上备份数据时,不会发生这种情况,onCreate()
即被调用并且“现在未准备好备份请求”没有出现。