It's easy to manage data backup on Android, and those who accidentally lose their device will be grateful for that feature. Backup data is stored securely in the cloud, and data is recovered only on devices with the same Google ID.
The following is a typical androidmanifest.xml file fragment:
<application
Android:allowbackup= "true"
Android:backupagent= "Mybackupagent"
android:icon= "@drawable/ic_launcher"
Android:label= "@string/app_name"
Android:theme= "@style/apptheme" >
<meta-data android:name= "Com.google.android.backup.api_key"
Android:value= "Backup-key_string"/>
.......
</application>
To open the backup function for an application, you only need to specify the class name of the backup agent in the Android:backupagent property. This class handles backup and recovery of application data. The Meta_data attribute in the preceding section specifies that the API key is registered in the Google Backup service. The specific registration site is: https://developer.android.com/google/backup/signup.html.
After registering and acquiring the API key, assign it to the Android:value property, as shown above. Although the key is bound to the application's package name and cannot be used for other applications, developers should be careful not to share it publicly in any code that is published.
The following class is a simple backup agent for backing up and restoring default preference files.
Note: the preference file obtained from preferencemanager.getdefaultpreferences () is not indicated in the <package-name>_preferences,api document. Understanding this is a great help in backing up your preference files.
public class Mybackupagent extends backupagenthelper{
public static final String prefs_backup_key= "Prefsbackup";
@Override
public void OnCreate () {
Super.oncreate ();
Sharedpreferencesbackuphelper sharedpreferencesbackuphelper=new Sharedpreferencesbackuphelper (This, Getpackagename () + "_preferences");
Addhelper (Prefs_backup_key,sharedpreferencesbackuphelper);
}
}
The Backupagenthelper class automatically backs up and restores the selected preferences file. You can also use the Filebackuphelper class to add backups for other regular files.
Google provides backup agents for Android apps for a small amount of data. Although it is technically feasible to back up the SQLite database, it is better to first convert the contents of the database into a serialized format, then compress the contents and finally back up the files.
The Android SDK provides the Bmgr command-line tool, which allows backup and recovery to be enforced on the application. This is useful for developing applications because it can be used to check if everything is OK.
Android App Data backup