Data backup

689 views
625 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
689
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
12
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Data backup

  1. 1. Data BackupKewang
  2. 2. What is Backup Service ?Backup data to the cloudRestore data when reinstalled appRequires API Level 8+ 2
  3. 3. Getting Started 3
  4. 4. Getting StartedRegister service & declare agentExtend backup agent BackupAgent BackupAgentHelper 4
  5. 5. Register service & declare agent 5
  6. 6. Register service & declare agent<manifest> <application android:backupAgent="ExampleAgent" > <meta-dataandroid:name="com.google.android.backup.api_key"android:value="AEdPqrEAAAAIW4p30C1GTNjzBOqWrb0clI7_OCWxm3ddIgkKhw" /> </application></manifest> backupAgent backup.api_key 6
  7. 7. Extend backup agentBackupAgent Version data format Back up the portions of data Back up data in a database 7
  8. 8. Extend BackupAgentpublic class ExampleAgent extends BackupAgent { @Override public void onBackup(ParcelFileDescriptor oldState,BackupDataOutput data, ParcelFileDescriptor newState) { // TODO } @Override public void onRestore(BackupDataInput data, intappVersionCode, ParcelFileDescriptor newState) { // TODO }} 8
  9. 9. Extend BackupAgentonBackup ParcelFileDescriptor oldState read-only last backup state, may be null contains representation of the data from the last onBackups newState BackupDataOutput data use to deliver backup data ParcelFileDescriptor newState record the final backup state after writing data 9
  10. 10. Extend BackupAgentonRestore BackupDataInput data can read backup data int appVersionCode Manifest attribute when data was backed up can cross-check app version ParcelFileDescriptor newState write the state of data 10
  11. 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(); read local fileif (!doBackup) { is newest doBackup = compareStateFile(oldState);} 11
  12. 12. Implement onBackup (2/2)if (doBackup) { ByteArrayOutputStream buf = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(buf); out.writeInt(mFilling); out.writeBoolean(mAddMayo); out.writeBoolean(mAddTomato); back up byte[] buffer = buf.toByteArray(); int len = buffer.length; data.writeEntityHeader(APP_DATA_KEY, len); data.writeEntityData(buffer, len);}writeStateFile(newState); 12
  13. 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 13
  14. 14. Implement onRestore (2/3)if (APP_DATA_KEY.equals(key)) { byte[] buf = new byte[size]; data.readEntityData(buf, 0, size); ByteArrayInputStream is = newByteArrayInputStream(buf); DataInputStream in = new DataInputStream(is); mFilling = in.readInt(); mAddMayo = in.readBoolean(); mAddTomato = in.readBoolean(); data is valid, read it 14
  15. 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();} } write data to local filewriteStateFile(newState); 15
  16. 16. Another backup agentBackupAgentHelper Back up SharedPreferences Back up a file 16
  17. 17. Extend BackupAgentHelperpublic class ExampleAgent extends BackupAgentHelper { @Override public void onCreate() { FileBackupHelper helper = new FileBackupHelper(this,"file"); addHelper("key", helper); }}can also useSharedPreferencesBackupHelper 17
  18. 18. How to using & testing it ? 18
  19. 19. How to using & testing it ?mBackupManager.dataChanged(); Request backup 19
  20. 20. How to using & testing it ?mBackupManager.requestRestore(new RestoreObserver() { @Override public void restoreStarting(int numPackages) { } @Override public void onUpdate(int nowBeingRestored,String currentPackage) { } Request restore @Override public void restoreFinished(int error) { }}); 20
  21. 21. How to using & testing it ?adb shell bmgr runadb shell bmgr backup <package>adb shell bmgr restore <package>adb shell bmgr wipe <package>adb shell bmgr enable <boolean> 21
  22. 22. FAQStorage size ? unknownBackup data TTL ? unknownCan synchronizing ? CANNOT 22
  23. 23. ReferencesAndroid Backup ServiceData BackupUsing the Backup APISample Project->BackupRestore 23
  24. 24. 24

×