无法调用BackupAgentHelper的onBackup()方法

时间:2011-08-19 11:12:04

标签: android android-backup-service

我正在上课,它会备份数据并恢复数据。我已经参考了api演示中给出的示例。但它根本不起作用。

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

Android Manifest.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>

MyActivity.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();
               }

        }
    }
}

2 个答案:

答案 0 :(得分:0)

查看docs:如果您延长BackupAgentHelper,那么在onCreate中,您必须注册一个或多个BackupHelper instances才能执行实际工作。如果你不这样做,我假设框架会忽略你的课程,而不是打电话给onBackuponRestore

编辑:您需要实施BackupHelper,然后在BackupAgentHelper的{​​{1}}方法中,您需要致电addHelper

答案 1 :(得分:0)

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

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

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