Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BackUpAgentHelperClass is not getting called

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?

like image 973
abh22ishek Avatar asked Apr 11 '15 17:04

abh22ishek


1 Answers

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.

like image 151
Yoni Gross Avatar answered Oct 03 '22 11:10

Yoni Gross