将共享首选项文件备份到云端

时间:2017-10-16 12:59:52

标签: android sharedpreferences android-backup-service

我正在尝试备份我的共享首选项文件,制作了BackupAgentHelper -

public class MyPrefsBackupAgent extends BackupAgentHelper {
// The name of the SharedPreferences file
static final String PREFS = "favorites";

// A key to uniquely identify the set of backup data
static final String PREFS_BACKUP_KEY = "prefs";

// Allocate a helper and add it to the backup agent
@Override
public void onCreate() {
    SharedPreferencesBackupHelper helper = new SharedPreferencesBackupHelper(this, PREFS);
    addHelper(PREFS_BACKUP_KEY, helper);
}
MainActivity上的

-

bm = new BackupManager(getApplicationContext());

    public static void requestBackup() {
    bm.dataChanged();
}

并在保存调用requestBackup方法的共享首选项时 -

        editor.commit();
    MainActivity.requestBackup();

显示如下 -

    <application
    android:allowBackup="true"
    android:backupAgent="MyPrefsBackupAgent">

        <meta-data android:name="com.google.android.backup.api_key"
        android:value="AEdPqrEAAAAI1Ap52upiK6qBdhShe4Diyzti2CLyayVouApfKQ" />

它不起作用,我做错了什么?

0 个答案:

没有答案