0

我有问题,否则我不会在这里。

我正在做备份服务,但到目前为止,它根本不起作用。不知道这是什么问题,但是在测试时(通过模拟器或通过电话,解释方式),数据将无法恢复。也许有人可以帮忙?

MyAppsBackupAgent

public class AppsBackupAgent extends BackupAgentHelper {

// The name of the SharedPreferences file
static final String PREFS = "uidpref";

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

// 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);
}

@Override
public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
        ParcelFileDescriptor newState) throws IOException {
    // TODO Auto-generated method stub

}

@Override
public void onRestore(BackupDataInput data, int appVersionCode,
        ParcelFileDescriptor newState) throws IOException {
    // TODO Auto-generated method stub

}

存储发生在 mainactivity 中:

SharedPreferences UIDpref = getSharedPreferences("uidpref", 0);
    Log.e("CODE", preferences.getBoolean("IDgenerated", false)+""); 
    BackupManager mBackupManager = new BackupManager(getApplicationContext());

    if(!preferences.getBoolean("IDgenerated", false)){
        SharedPreferences.Editor UIDedit = UIDpref.edit();
        String rnd = GenerateUID(30);
        UIDedit.putString("UID", rnd);
        UIDedit.putBoolean("IDgenerated", true);
        UIDedit.commit();
        mBackupManager.dataChanged();
    }

并体现:

<application
    android:backupAgent="ee.elven.katja.AppsBackupAgent"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"  >

...

4

1 回答 1

0

这是一个老问题,但也许你仍然需要一个答案。在您的 AppsBackupAgent 实现中删除onRestore()onBackup()方法。

继承BackupAgentHelper的实现使用调度程序将恢复/备份事件传递给注册的助手。您的实现只是禁用了这项工作。

于 2014-06-05T14:24:52.527 回答