一、支持allowBackup
在安卓Manifest.xml文件中有一个非常重要的标签,就是android:allowBackup
属性。这个属性决定了是否允许系统备份应用数据,是在开发应用时需要特别注意的一个点。
首先我们看看这个属性的声明方式:
android:allowBackup=["true" | "false"]
其中,"true"表示允许备份;"false"表示禁止备份。默认情况下,该属性为true。 如果应用程序中没有声明这个属性,系统默认为true,就允许我们应用程序的数据通过备份工具(如adb backup)进行备份。 那么,我们为什么需要备份呢? 一方面,开发者可能需要这些数据来测试他们的应用程序,比如你可以恢复用户在你的应用程序中输入的数据,从而进行验证。 另一方面,如果用户升级了他们的设备,或者因某种原因重新安装应用程序,那么他们可以从备份中恢复他们的数据,而不是失去它们。
二、编程中的allowBackup什么意思
如果你不想让应用程序中的数据被备份,可以将这个属性设置为false。通常,在开发应用程序时,我们希望保存用户数据并在必要时使用它们,这样我们就需要在Manifest文件中声明这个属性。 一般情况下,直接在Manifest文件中添加属性即可,例如:
<application
android:name=".MyApplication"
android:allowBackup="false"
...>
这里我们声明了一个名为MyApplication的类,并将属性allowBackup
设置为false,这样我们就禁止了应用程序的备份。
三、allowBackup怎么办
那么,如果我们需要备份应用程序中的数据呢?此时我们需要做一些配置。让我们进一步探讨一下:
四、安卓allowBackup选取
1、声明备份数据类型
首先,我们需要声明哪些数据应该被备份。这是通过在Manifest文件中定义<backupagent>
元素来实现的:
<application
android:name=".MyApplication"
android:allowBackup="true"
...>
<backupagent android:name=".MyBackupAgent" android:allowBackup="true" />
...
</application>
这里我们声明了一个名为MyBackupAgent的备份代理,并将属性allowBackup
设置为true。我们也可以在backupagent
元素中定义备份的数据类型:
<application
android:name=".MyApplication"
android:allowBackup="true"
...>
<backupagent
android:name=".MyBackupAgent"
android:allowBackup="true"
android:backupAgent=".MyBackupAgent"
android:killAfterRestore="false">
<include
android:domain="sharedPreference"
android:path="my_prefs.xml" />
<exclude
android:domain="file"
android:path="my_file.txt" />
</backupagent>
...
</application>
这里我们定义了两个<include>
和<exclude>
元素,用于指定哪些数据类型应该被包含在备份中,哪些应该被排除。
2、创建备份代理类
接下来我们需要创建一个备份代理类,并在类中实现备份和恢复数据方法。备份代理通常继承自BackupAgent
类,并重写onBackup()
和onRestore()
方法。
public class MyBackupAgent extends BackupAgent {
static final String PREFS_BACKUP_KEY = "my_prefs";
static final String FILE_BACKUP_KEY = "my_file";
@Override
public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data, ParcelFileDescriptor newState) throws IOException {
// 备份shared preferences
SharedPreferences prefs = getSharedPreferences("my_prefs", MODE_PRIVATE);
byte[] bytes = prefs.getString("my_prefs", "").getBytes();
data.writeEntityHeader(PREFS_BACKUP_KEY, bytes.length);
data.writeEntityData(bytes, bytes.length);
// 备份文件
File file = new File(getFilesDir(), "my_file.txt");
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[(int) file.length()];
fis.read(buffer);
data.writeEntityHeader(FILE_BACKUP_KEY, buffer.length);
data.writeEntityData(buffer, buffer.length);
}
@Override
public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState) throws IOException {
while (data.readNextHeader()) {
String key = data.getKey();
int dataSize = data.getDataSize();
byte[] buffer = new byte[dataSize];
data.readEntityData(buffer, 0, dataSize);
if (PREFS_BACKUP_KEY.equals(key)) {
String prefsString = new String(buffer);
SharedPreferences prefs = getSharedPreferences("my_prefs", MODE_PRIVATE);
prefs.edit().putString("my_prefs", prefsString).commit();
} else if (FILE_BACKUP_KEY.equals(key)) {
FileOutputStream fos = openFileOutput("my_file.txt", MODE_PRIVATE);
fos.write(buffer);
fos.close();
}
}
}
}
这里,我们重写了onBackup()
和onRestore()
方法。onBackup()
方法中,我们备份了shared preferences和一个文件。onRestore()
方法中,我们恢复了这两个数据类型。
注意,在onBackup()
方法中,我们需要确保要备份的数据以字节数组的形式写入BackupDataOutput
。在onRestore()
方法中,我们需要从BackupDataInput
中读取备份数据,并根据键值类型进行恢复处理。
3、跟踪备份和恢复过程
假设我们已经完成了备份代理的设置和实现,并想查看备份和恢复过程中发生的情况。我们可以使用命令行工具来执行备份和还原操作。 下面是一个备份命令的例子:
adb backup -f myapp.ab com.example.myapp
在这个例子中,我们使用adb工具,在myapp.ab
文件中备份了com.example.myapp
应用程序的数据。注意,我们要使用应用程序的包名来指定应用程序。
还原数据的命令如下:
adb restore myapp.ab
我们可以在应用程序的onRestore()
方法中打印日志来跟踪数据还原过程。
本文代码示例:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp">
<application
android:name=".MyApplication"
android:allowBackup="false"
...>
<backupagent android:name=".MyBackupAgent" android:allowBackup="true" />
...
</application>
</manifest>
public class MyBackupAgent extends BackupAgent {
static final String PREFS_BACKUP_KEY = "my_prefs";
static final String FILE_BACKUP_KEY = "my_file";
@Override
public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data, ParcelFileDescriptor newState) throws IOException {
// 备份shared preferences
SharedPreferences prefs = getSharedPreferences("my_prefs", MODE_PRIVATE);
byte[] bytes = prefs.getString("my_prefs", "").getBytes();
data.writeEntityHeader(PREFS_BACKUP_KEY, bytes.length);
data.writeEntityData(bytes, bytes.length);
// 备份文件
File file = new File(getFilesDir(), "my_file.txt");
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[(int) file.length()];
fis.read(buffer);
data.writeEntityHeader(FILE_BACKUP_KEY, buffer.length);
data.writeEntityData(buffer, buffer.length);
}
@Override
public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState) throws IOException {
while (data.readNextHeader()) {
String key = data.getKey();
int dataSize = data.getDataSize();
byte[] buffer = new byte[dataSize];
data.readEntityData(buffer, 0, dataSize);
if (PREFS_BACKUP_KEY.equals(key)) {
String prefsString = new String(buffer);
SharedPreferences prefs = getSharedPreferences("my_prefs", MODE_PRIVATE);
prefs.edit().putString("my_prefs", prefsString).commit();
} else if (FILE_BACKUP_KEY.equals(key)) {
FileOutputStream fos = openFileOutput("my_file.txt", MODE_PRIVATE);
fos.write(buffer);
fos.close();
}
}
}
}