Android App Data backup

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.