在讲解该sample之前需要和大家聊聊这个东东。我们看下官方文档怎么解说这个。
Android's
backup
service allows you to copy your persistent application data to a remote "cloud" storage, in order to provide a restore point for the application data and settings. If a user performs a factory reset or converts to a new Android-powered device, the system automatically restores your backup data when the application is re-installed. This way, your users are not required to reproduce their previous data or application settings. This process is completely transparent to the user and does not affect the functionality or user experience in your application.翻译就不用我来了吧,主要是说把我们应用程序的数据存储到云端服务器上面。OK,至于更详细的你们可以查看一下文档,我在这里还想提点,如果你文档是2.2的,你会发现一点,这个介绍的前面有个new的标签。哈哈,知道了吧,这就是2.2的一个新特性。很多面试的时候会问你个个版本有哪些新特性,你可以说2.2有这个。然后我们来看看官方教我们怎么用
To implement a backup agent, you must:
- Declare your backup agent in your manifest file with the
android:backupAgent
attribute.- Define a backup agent by either:
- Extending BackupAgent
The
BackupAgent
class provides the central interface with which your application communicates with the Backup Manager. If you extend this class directly, you must overrideonBackup()
andonRestore()
to handle the backup and restore operations for your data.Or
- Extending BackupAgentHelper
The
BackupAgentHelper
class provides a convenient wrapper around theBackupAgent
class, which minimizes the amount of code you need to write. In yourBackupAgentHelper
, you must use one or more "helper" objects, which automatically backup and restore certain types of data, so that you do not need to implementonBackup()
andonRestore()
.Android currently provides backup helpers that will backup and restore complete files from
SharedPreferences
and internal storage.用法很简单。第一个就是要在AndroidManifest.xml有这个android:backupAgent这个属性。看看BackupRestore的这个文件
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android.backuprestore" android:versionCode="1" android:versionName="1.0"> <!-- The backup/restore mechanism was introduced in API version 8 --> <uses-sdk android:minSdkVersion="8"/> <application android:label="Backup/Restore" android:backupAgent="ExampleAgent"> <activity android:name="BackupRestoreActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
看到红色的部分了么?这既是第一步。呵呵,so easy。
然后就是扩展(继承)BackupAgent 和BackupAgentHelper 。之后在我们程序中使用这些扩展类。至于详细的,看我后面的教程。今天只是稍微了解一下这个东西,给你们掉下口味。哈哈