Android Application Data Backup

Source: Internet
Author: User

Android Application Data Backup

On Android, you can easily manage data backups. Users who accidentally lose devices will be grateful for this feature. Backup data is stored securely on the cloud and only restored on devices with the same Google ID.

The following is a typical AndroidManifest. xml file segment:

Android: allowBackup = "true"

Android: backupAgent = "MyBackupAgent"

Android: icon = "@ drawable/ic_launcher"

Android: label = "@ string/app_name"

Android: theme = "@ style/AppTheme">

Android: value = "backup-key_string"/>

.......

 

To enable the backup function of an application, you only need to specify the backup proxy class name in the android: backupAgent attribute. This class processes the backup and recovery of application data. The meta_data attribute of the secret specifies the API key to be registered in the Google backup service. The specific registration URL is: https://developer.android.com/google/backup/signup.html.

After registering and obtaining the API key, assign it to the android: value Attribute, as shown above. Although the key is bound to the package name of the application and cannot be used for other applications, developers should be careful not to share it publicly in any released code.

The following class is a simple backup proxy used to back up and restore default preference files.

Note: The preference file name obtained from PreferenceManager. getdefaprepreferences () is _ Preferences, which is not described in the API documentation, is of great help to backup preferences.

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 preference file. You can also use the FileBackupHelper class to add backups for other common files.

The backup proxy provided by Google for Android apps is suitable for a small amount of data. Although it is technically feasible to back up the SQLite database, it is best to convert the database content into a serialized format, compress the content, and finally back up the file.

The Android SDK provides the bmgr command line tool that allows forced backup and recovery of applications. This is useful for application development because it can be used to check whether everything works normally.

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.