0

I am having class which takes backup of data and restore the same. I have referred the example given in api demos. But it does not work at all.

Can anybody help me over this issue?

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

        }
    }
}
HitOdessit
  • 7,198
  • 4
  • 36
  • 59
Richa
  • 3,165
  • 1
  • 22
  • 26

2 Answers2

0

You should build your backup agent using BackupAgentHelper if you want to back up complete files (from either SharedPreferences or internal storage). For each helper you want to add to your BackupAgentHelper, you must do the following during your onCreate() method:

  1. Instantiate in instance of the desired helper class. In the class constructor, you must specify the appropriate file(s) you want to backup.
  2. Call addHelper() to add the helper to your BackupAgentHelper.

Sample:

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);
    }
}
Pang
  • 9,564
  • 146
  • 81
  • 122
0

Look at the docs: if you extend BackupAgentHelper then in onCreate you have to register one or more BackupHelper instances to perform the actual work. If you don't do that I'm assuming the framework will ignore your class and simply not call onBackup or onRestore.

EDIT: you need to implement a BackupHelper and then in your BackupAgentHelper's onCreate method you need to call addHelper.

Femi
  • 64,273
  • 8
  • 118
  • 148
  • i dont get u can u plz tell the changes which i need to make in my files..... ???? – Richa Aug 20 '11 at 07:07
  • I have added that two lines bt it still dont call the class:MyBackupAgent FileBackupHelper fileHelper = new FileBackupHelper(getBaseContext(),"Myfile"); addHelper(FILES_BACKUP_KEY, fileHelper); – Richa Aug 20 '11 at 07:44