0

我正在上课,它需要备份数据并恢复相同的数据。我参考了 api demos 中给出的示例。但它根本不起作用。

有人可以帮我解决这个问题吗?

安卓清单.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.kpbird.backupdemo"
      android:versionCode="1"
      android:versionName="1.0">


    <application android:icon="@drawable/icon" android:label="@string/app_name" android:backupAgent="MyBackupAgent" android:restoreAnyVersion="true" android:allowBackup="true" android:enabled="true">
        <activity android:name=".BackupServiceDemo"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <meta-data android:name="com.google.android.backup.api_key" android:value="AEdPqrEAAAAIAjALiYV5vv5cRGD5L649XByQMnFFYfApskNIfg" />

    </application>
</manifest>

我的活动.class

public class BackupRestoreActivity extends Activity implements OnClickListener {

 BackupManager mBackupManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mBackupManager = new BackupManager(this);
        mBackupManager.dataChanged();

}
}

MyBackupAgent.class

public class MyBackupAgent extends BackupAgentHelper {


    static final String MY_PREFS_BACKUP_KEY = "AEdPqrEAAAAIAjALiYV5vv5cRGD5L649XByQMnFFYfApskNIfg";
    static final String APP_DATA_KEY = "mydata";
    String TAG = this.getClass().getSimpleName();
    @Override
    public void onCreate() {

    Log.i("MyBackupAgent >>>>>> ","into Oncreate() of my Backup Agent >>>>");


    }

    @Override
    public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data,ParcelFileDescriptor newState) throws IOException {

            Log.i(TAG, "I m onBackup");
          ByteArrayOutputStream bufStream = new ByteArrayOutputStream();
          DataOutputStream outWriter = new DataOutputStream(bufStream);
          outWriter.writeUTF("Hello Backup Service!");

          byte[] buffer = bufStream.toByteArray();
          int len = buffer.length;
          data.writeEntityHeader(APP_DATA_KEY, len);
          data.writeEntityData(buffer, len);

          FileOutputStream outstream = new FileOutputStream(newState.getFileDescriptor());
          DataOutputStream out = new DataOutputStream(outstream);
          out.writeUTF("Hello Backup Service!");
          Log.i(TAG, "Backup Message Completed");

    }

    @Override
    public void onRestore(BackupDataInput data, int appVersionCode,ParcelFileDescriptor newState) throws IOException {
        Log.i(TAG, "I m onRestore");
        while (data.readNextHeader()) {
               String key = data.getKey();
               int dataSize = data.getDataSize();
               if(key.equals(APP_DATA_KEY)){
                   byte[] dataBuf = new byte[dataSize];
                    data.readEntityData(dataBuf, 0, dataSize);
                    ByteArrayInputStream baStream = new ByteArrayInputStream(dataBuf);
                    DataInputStream in = new DataInputStream(baStream);
                    String read = in.readUTF();
                    Log.i(TAG, "Restored Message :" + read);
               }
               else{
                   data.skipEntityData();
               }

        }
    }
}
4

2 回答 2

0

如果要备份完整的文件(从 SharedPreferences 或内部存储),则应使用 BackupAgentHelper 构建备份代理。对于要添加到 BackupAgentHelper 的每个帮助程序,您必须在 onCreate() 方法期间执行以下操作:

  1. 在所需帮助类的实例中实例化。在类构造函数中,您必须指定要备份的适当文件。
  2. 调用 addHelper() 将帮助程序添加到 BackupAgentHelper。

样本:

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

    // 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);
    }
}
于 2015-01-16T12:15:58.513 回答
0

查看文档:如果您进行扩展BackupAgentHelper,则onCreate必须注册一个或多个BackupHelper 实例才能执行实际工作。如果您不这样做,我假设框架将忽略您的课程,并且根本不会调用onBackupor onRestore

编辑:你需要实现 aBackupHelper然后在你BackupAgentHelperonCreate方法中你需要调用addHelper

于 2011-08-20T07:01:32.897 回答