Introduction
- During a backup operation, the Android's Backup Manager queries your application for backup data, then hands it to a backup transport that then delivers the data to the cloud storage.
- During a restore and during the retrieval operation, the Backup Manager retrieves the backup data from the backup transport and returns it to your application so your application can restore the data to the device.
Client-Side Component
Backup Agent
- Declare your backup agent in your manifest file with the Android: backupAgent attribute.
- Register your application with a backup service. Google offers Android Backup Service as a backup service for most Android-powered devices that require registration of an application for it to work. Any other backup services available might also require you to register to store your data on their servers.
Define a Backup Agent by Either
Extending BackupAgent
The BackupAgent class provides the central interface that your application communicates with the Backup Manager. If you extend this class directly, you must override onBackup() and on Restore() to handle the backup and restore operations for your data.Or-
Extending BackupAgentHelper
The BackupAgentHelper class provides a convenient wrapper around the BackupAgent class that minimizes the amount of code you need to write. In your BackupAgentHelper, you must use one or more "helper" objects that automatically backup and restore certain types of data, so that you do not need to implement onBackup() and onRestore(). Android currently provides backup helpers that will backup and restore complete files from SharedPreferences and internal storage.
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.fragment"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk
- android:minSdkVersion="16"
- android:targetSdkVersion="21" />
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme"
- android:label="MyApplication"
- android:backupAgent="MyBackupAgent">
- <activity
- android:name=".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>
- </application>
- </manifest>
Registering the Android Backup Service
- <application android:label="MyApplication"
- android:backupAgent="MyBackupAgent">
- ...
- <meta-data android:name="com.google.android.backup.api_key"
- android:value="AEdPqrEAAAAIDaYEVgU6DJnyJdBmU7KLH3kszDXLv_4DIsEIyQ" />
- </application>
Extending BackupAgent
- onBackup()
The Backup Manager calls this method after you request a backup. In this method, you read your application data from the device and the data you want to back up to the Backup Manager, as described below in Performing backup.
- onRestore()The Backup Manager calls this method during a restore operation. When it calls this method, the Backup Manager delivers your backup data that you then restore to the device, as described below in Performing restore.
Parameters es by onBackup() method
- oldStateAn open, read-only ParcelFileDescriptor pointing to the last backup state provided by your application. This is not the backup data from cloud storage, but a local representation of the data that was backed up the last time onBackup(). Because onBackup() does not allow you to read existing backup data in the cloud storage, you can use this local representation to determine whether your data has changed since the last backup.
- data
A BackupDataOutput object that you use to deliver your backup data to the Backup Manager.
- newStateAn open, read/write ParcelFileDescriptor pointing to a file in which you must write a representation of the data that you delivered to data (a representation can be as simple as the last-modified timestamp for your file). This object is returned as oldState the next time the Backup Manager calls your onBackup() method. If you do not write your backup data to newState, then oldState will point to an empty file the next time Backup Manager calls onBackup().
Get old state input stream
Code
- // Get the oldState input stream
- FileInputStream instream = new FileInputStream(oldState.getFileDescriptor());
- DataInputStream in = new DataInputStream(instream);
- try {
- // Get the last modified timestamp from the state file and data file
- long stateModified = in.readLong();
- long fileModified = mDataFile.lastModified();
- if (stateModified != fileModified) {
- // The file has been modified, so do a backup
- // Or the time on the device changed, so be safe and do a backup
- } else {
- // Don't back up because the file hasn't changed
- return;
- }
- } catch (IOException e) {
- // Unable to read state file... be safe and do a backup
- }
Restoring the Data
- Get the entity key with getKey().
- Compare the entity key to a list of known key values that you should have declared as static final strings inside your BackupAgent class. When the key matches one of your known key strings, enter into a statement to extract the entity data and save it to the device.
- Get the entity data size with getDataSize() and create a byte array of that size.
- Call readEntityData() and it the byte array that is where the data will go and specify the start offset and the size to read.
- Your byte array is now full and you can read the data and write it to the device however you like.
- @Override
- public void onRestore(BackupDataInput data, int appVersionCode,
- ParcelFileDescriptor newState) throws IOException {
- // There should be only one entity, but the safest
- // way to consume it is using a while loop
- while (data.readNextHeader()) {
- String key = data.getKey();
- int dataSize = data.getDataSize();
- // If the key is ours (for saving top score). Note this key was used when
- // we wrote the backup entity header
- if (TOPSCORE_BACKUP_KEY.equals(key)) {
- // Create an input stream for the BackupDataInput
- byte[] dataBuf = new byte[dataSize];
- data.readEntityData(dataBuf, 0, dataSize);
- ByteArrayInputStream baStream = new ByteArrayInputStream(dataBuf);
- DataInputStream in = new DataInputStream(baStream);
- // Read the player name and score from the backup data
- mPlayerName = in.readUTF();
- mPlayerScore = in.readInt();
- // Record the score on the device (to a file or something)
- recordScore(mPlayerName, mPlayerScore);
- } else {
- // We don't know this entity key. Skip it. (Shouldn't happen.)
- data.skipEntityData();
- }
- }
- // Finally, write to the state blob (newState) that describes the restored data
- FileOutputStream outstream = new FileOutputStream(newState.getFileDescriptor());
- DataOutputStream out = new DataOutputStream(outstream);
- out.writeUTF(mPlayerName);
- out.writeInt(mPlayerScore);
- }

Tom MohanPosted Mar 1, 2015, 7:50 PM
Nice
Vithal WadjePosted Feb 26, 2015, 11:25 AM
nice sir
Sibeesh VenuPosted Feb 26, 2015, 2:39 AM
Good One.