You may have noticed that you can easily back up all your data on your Android device. You can also implement the backup facility on your application. To do that you will need to specify the keys in the AndroidManifest.xml file.
You can refer to the following example for a better understanding:
1.Let’s start by creating an application in Android Studio under a package com.firstapp.ashulakhwan.greatlearning
2.In the second step, you will need to register your application on Google backup services.
3.After registration, modify the AndroidManifest.xml file with the following content:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.firstapp.ashulakhwan.greatlearning" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:backupAgent="MyBackUpPlace"
android:theme="@style/AppTheme" >
<activity
android:name="com.firstapp.ashulakhwan.greatlearning.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="AdAPqsEABAAIEzlxFWyGgNzywqBe2b6TsmLsp5KshhPA-ZSwgj" />
</application>
</manifest>
4.Next, you will be required to create a backup agent class in the same AndroidManifest.xml file.
import android.app.backup.BackupAgentHelper;
import android.app.backup.SharedPreferencesBackupHelper;
public class MyBackUpPlace extends BackupAgentHelper {
static final String File_Prefrences = "my_files";
static final String PREFS_BACKUP_KEY = "CREATEBACKUP";
@Override
public void onCreate() {
SharedPreferencesBackupHelper helper_tool = new SharedPreferencesBackupHelper(this,
File_Prefrences);
addHelper(PREFS_BACKUP_KEY, helper_tool);
}
}
5. The last step will include the testing of the application to verify the results.