Upload
mu-chun-wang
View
529
Download
5
Tags:
Embed Size (px)
Citation preview
Data Backup
Kewang
2
Backup data to the cloud
Restore data when reinstalled app
Requires API Level 8+
What is Backup Service ?
3
Getting Started
4
Getting Started
Register service & declare agent
Extend backup agent
BackupAgent
BackupAgentHelper
5
Register service & declare agent
6
Register service & declare agent<manifest>
<application android:backupAgent="ExampleAgent" > <meta-data android:name="com.google.android.backup.api_key" android:value="AEdPqrEAAAAIW4p30C1GTNjzBOqWrb0clI7_OCWxm3ddIgkKhw" />
</application>
</manifest> backupAgentbackup.api_key
7
Extend backup agent
BackupAgent
Version data format
Back up the portions of data
Back up data in a database
8
Extend BackupAgent
public class ExampleAgent extends BackupAgent { @Override public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data, ParcelFileDescriptor newState) { // TODO }
@Override public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState) { // TODO }}
9
Extend BackupAgent
onBackupParcelFileDescriptor oldState
read-only last backup state, may be nullcontains representation of the datafrom the last onBackup's newState
BackupDataOutput datause to deliver backup data
ParcelFileDescriptor newStaterecord the final backup state after writing data
10
Extend BackupAgent
onRestoreBackupDataInput data
can read backup data
int appVersionCodeManifest attribute when data was backed upcan cross-check app version
ParcelFileDescriptor newStatewrite the state of data
11
Implement onBackup (1/2)boolean doBackup = (oldState == null);
synchronized (BackupRestoreActivity.sDataLock) { RandomAccessFile f = new RandomAccessFile(mFile, "r");
mFilling = f.readInt(); mAddMayo = f.readBoolean(); mAddTomato = f.readBoolean();
f.close();}
if (!doBackup) { doBackup = compareStateFile(oldState);}
read local fileis newest
12
if (doBackup) { ByteArrayOutputStream buf = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(buf);
out.writeInt(mFilling); out.writeBoolean(mAddMayo); out.writeBoolean(mAddTomato);
byte[] buffer = buf.toByteArray(); int len = buffer.length;
data.writeEntityHeader(APP_DATA_KEY, len); data.writeEntityData(buffer, len);}
writeStateFile(newState);
Implement onBackup (2/2)
back up
13
Implement onRestore (1/3)
String key = data.getKey();int size = data.getDataSize();
if (!APP_DATA_KEY.equals(key)) { data.skipEntityData();}
data is invalid, skip it
14
Implement onRestore (2/3)
if (APP_DATA_KEY.equals(key)) { byte[] buf = new byte[size];
data.readEntityData(buf, 0, size);
ByteArrayInputStream is = new ByteArrayInputStream(buf); DataInputStream in = new DataInputStream(is);
mFilling = in.readInt(); mAddMayo = in.readBoolean(); mAddTomato = in.readBoolean();
data is valid, read it
15
Implement onRestore (3/3)
synchronized (BackupRestoreActivity.sDataLock) { RandomAccessFile f = new RandomAccessFile(mFile, "rw");
f.setLength(0L); f.writeInt(mFilling); f.writeBoolean(mAddMayo); f.writeBoolean(mAddTomato);
f.close(); }}
writeStateFile(newState);
write data to local file
16
Another backup agent
BackupAgentHelper
Back up SharedPreferences
Back up a file
17
Extend BackupAgentHelper
public class ExampleAgent extends BackupAgentHelper { @Override public void onCreate() { FileBackupHelper helper = new FileBackupHelper(this, "file");
addHelper("key", helper); }}
can also use SharedPreferencesBackupHelper
18
How to using & testing it ?
19
How to using & testing it ?
mBackupManager.dataChanged();
Request backup
20
How to using & testing it ?
mBackupManager.requestRestore(new RestoreObserver() { @Override public void restoreStarting(int numPackages) { }
@Override public void onUpdate(int nowBeingRestored,String currentPackage) { }
@Override public void restoreFinished(int error) { }});
Request restore
21
How to using & testing it ?
adb shell bmgr run
adb shell bmgr backup <package>
adb shell bmgr restore <package>
adb shell bmgr wipe <package>
adb shell bmgr enable <boolean>
22
FAQ
Storage size ?
unknown
Backup data TTL ?
unknown
Can synchronizing ?
CANNOT
23
References
Android Backup Service
Data Backup
Using the Backup API
Sample Project->BackupRestore
24