27

I want to backup data in Android using MyBackUpAgent class which extends BackupAgentHelper. I am using SharedPreferences in order to store data.

My mainactivity code is:

public class MainActivity extends Activity {
    EditText inputtext; 
    TextView outputtext; 
    Button submit;   
    public static SharedPreferences sharedprefs;
    static final String File_Name_Of_Prefrences ="godplay_preferences";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);

        init();      
        sharedprefs=getSharedPreferences(File_Name_Of_Prefrences,MODE_PRIVATE);
        System.out.println("value="+sharedprefs.getString("Input",""));
        outputtext.setText(sharedprefs.getString("Input",""));


        submit.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                populateUI();
            }
        });
    }

    public void populateUI()
    {
        String savedinput=inputtext.getText().toString();
        System.out.println("savedinput="+savedinput);
        outputtext.setText(savedinput);
        sharedprefs=getSharedPreferences(File_Name_Of_Prefrences,MODE_PRIVATE);
        Editor editor=sharedprefs.edit();
        editor.putString("Input",inputtext.getText().toString());
        editor.commit();
        requestBackup();
    }

    private void init() throws ClassCastException
    {
        inputtext=(EditText) findViewById(R.id.edtInputText);
        outputtext=(TextView) findViewById(R.id.txtOutputText);
        submit=(Button) findViewById(R.id.btnSubmit);
    }

    public void requestBackup() {
        BackupManager bm = new BackupManager(getApplicationContext());
        bm.dataChanged();
    }
}

My MyBackUpAgent class:

public class MyBackUpAgent extends BackupAgentHelper{
static final String PREFS_BACKUP_KEY = "backup";
       String key_string="Hello World";

     @Override
       public void onCreate() {
    System.out.println("********************");
    SharedPreferencesBackupHelper helper = new SharedPreferencesBackupHelper(this,MainActivity.File_Name_Of_Prefrences);
    addHelper(PREFS_BACKUP_KEY, helper);

     } 
}

My mainfest.xml file:

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:restoreAnyVersion="false"
        android:backupAgent=".MyBackUpAgent"

        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.godplay.MainActivity"
            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="AEdPqrEAAAAIhjloadYCTPUNo3yPsSX6LKmziiumZiQVlEEdBA" />
    </application>
</manifest>

So far I have tried with bmgr tool to test, it is executing successfully with bmgr tool. However, on testing on Android device and emulator, back up is not happening, nor restoring.

Also, I have tested this on Android 5.1, Android 4.2, and Android 4.0 but still no luck.

It seems to me that my MyBackUpAgent class is never getting called, and I have tried breakpoints in MyBackUpAgent Class and validated it. Its never get hit.

What am I doing wrong?

Zamrony P. Juhara
  • 5,222
  • 2
  • 24
  • 40
abh22ishek
  • 2,631
  • 4
  • 27
  • 47

5 Answers5

1

Docs mention Conditions For Backup Schedule:

  • The user has enabled backup on the device in Settings > Backup & Reset.
  • At least 24 hours have elapsed since the last backup.
  • The device is idle and charging.
  • The device is connected to a Wi-Fi network. If the device is never connected to a wifi network, then Auto Backup never occurs.

If backup is working for you with bmgr tool but not on a real device / emulator, it's possible you are not meeting all those conditions, therefore backup never occur.

Yoni Gross
  • 816
  • 8
  • 16
0

In your AndroidManifest.xml file, try changing

android:backupAgent=".MyBackUpAgent"

with the fully qualified class name, i.e.

android:backupAgent="com.abh.utils.MyBackUpAgent"

but of course changing "com.abh.utils" with the name of the package MyBackUpAgent.java is in.

Luke
  • 2,187
  • 1
  • 18
  • 29
0

I had a similar problem and have searched everywhere with no luck. Finally found the solution. It seems that the BackupAgent needs to be in the top package with no preceding dot. So try changing:

android:backupAgent=".MyBackUpAgent"

to

android:backupAgent="MyBackUpAgent"
Karry
  • 1
0

You can refer to developer document,

https://developer.android.com/guide/topics/data/backup.html#PerformingBackup

A backup request does not result in an immediate call to your onBackup() method. Instead, the Backup Manager waits for an appropriate time

you can use "bmgr tool" to initiate immediate backup whiling developing your app.

nansjlee
  • 417
  • 2
  • 8
0

Make sure you call

adb shell bmgr run

to simulate the backup.

Also try using local transport to backup at any time:

adb shell bmgr transport android/com.android.internal.backup.LocalTransport
live-love
  • 48,840
  • 22
  • 240
  • 204