android实现本程序数据的备份与恢复
- - ITeye博客android中数据存储方式主要有四种,即SharePreferences,Sqlite3,文件和ContentProvider. 所以要对本程序的数据进行备份,其实也就是把这些数据文件拷贝到另外的目录,在恢复时再拷贝回来. 以下以我的一个程序为例,其中使用了SharePreferences及sqlite3两个数据存储的方法.
/* * @(#)Backup.java Project:UniversityTimetable * Date:2012-4-16 * * Copyright (c) 2011 CFuture09, Institute of Software, * Guangdong Ocean University, Zhanjiang, GuangDong, China. * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.lurencun.cfuture09.universityTimetable.backup.util; import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; import android.content.Context; import android.os.Environment; import android.widget.Toast; import com.sinaapp.msdxblog.androidkit.util.ResourceUtil; import com.sinaapp.msdxblog.androidkit.util.entity.ApkInfo; /** * @author Geek_Soledad ([email protected]) */ public class Backup { private String SHARED_PREFS; private String DATABASES; private String APP_PATH; private Context mContext; private String BACKUP_PATH; private String BACKUP_DATABASES; private String BACKUP_SHARED_PREFS; public Backup(Context context) { mContext = context; ApkInfo apkInfo = new ResourceUtil(context).getApkInfo(); APP_PATH = new StringBuilder("/data/data/").append(apkInfo.packageName) .toString(); SHARED_PREFS = APP_PATH + "/shared_prefs"; DATABASES = APP_PATH + "/databases"; if (Environment.MEDIA_MOUNTED.equals(Environment .getExternalStorageState())) { BACKUP_PATH = "/sdcard/lurencun/backup"; } else { BACKUP_PATH = "/com.lurencun/backup/"; Toast.makeText(mContext, "没有检测到SD卡,可能无法备份成功", Toast.LENGTH_SHORT) .show(); } BACKUP_PATH += apkInfo.packageName; BACKUP_DATABASES = BACKUP_PATH + "/database"; BACKUP_SHARED_PREFS = BACKUP_PATH + "/shared_prefs"; } /** * 备份文件 * * @return 当且仅当数据库及配置文件都备份成功时返回true。 */ public boolean doBackup() { return backupDB() && backupSharePrefs(); } private boolean backupDB() { return copyDir(DATABASES, BACKUP_DATABASES, "备份数据库文件成功:" + BACKUP_DATABASES, "备份数据库文件失败"); } private boolean backupSharePrefs() { return copyDir(DATABASES, BACKUP_DATABASES, "备份配置文件成功:" + BACKUP_SHARED_PREFS, "备份配置文件失败"); } /** * 恢复 * * @return 当且仅当数据库及配置文件都恢复成功时返回true。 */ public boolean doRestore() { return restoreDB() && restoreSharePrefs(); } private boolean restoreDB() { return copyDir(BACKUP_DATABASES, DATABASES, "恢复数据库文件成功", "恢复数据库文件失败"); } private boolean restoreSharePrefs() { return copyDir(BACKUP_SHARED_PREFS, SHARED_PREFS, "恢复配置文件成功", "恢复配置文件失败"); } private final void showToast(String msg) { Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show(); } /** * 复制目录 * * @param srcDir * 源目录 * @param destDir * 目标目录 * @param successMsg * 复制成功的提示语 * @param failedMsg * 复制失败的提示语 * @return 当复制成功时返回true, 否则返回false。 */ private final boolean copyDir(String srcDir, String destDir, String successMsg, String failedMsg) { try { FileUtils.copyDirectory(new File(srcDir), new File(destDir)); } catch (IOException e) { e.printStackTrace(); showToast(failedMsg); return false; } showToast(successMsg); return true; } }