public class DataBaseUtils {
// 当前数据库地址
private String DB_PATH;
// 备份后数据库保存地址
private String DB_BACKUP_PATH;
private Context context;
private MessageShow ms;
// 备份成功状态
private final int BACKUP_OK = 1;
// 备份失败状态
private final int BACKUP_FAIL = -1;
// 恢复成功状态
private final int RECOVER_OK = 2;
// 恢复失败状态
private final int RECOVER_FAIL = -2;
public interface MessageShow{
public void onPepare();
public void onSuccess();
public void onFail();
}
public DataBaseUtils(Context context) {
this.context = context;
}
/**
* @Title: doDataBackUp
* @Description: TODO(数据备份)
* @param
* @return void 返回类型
* @author fenglei015@deppon.com/273219
* @throws
*/
public void doDataBackUp(String DB_PATH, String DB_BACKUP_PATH,MessageShow ms) {
this.DB_PATH = DB_PATH;
this.DB_BACKUP_PATH = DB_BACKUP_PATH;
this.ms=ms;
new BackUpTask().execute();
}
/**
* @ClassName: BackUpTask
* @Description: TODO(数据库备份异步任务)
* @author fenglei015@deppon.com/273219
* @date 2015-12-25 上午11:10:58
*
*/
@TargetApi(Build.VERSION_CODES.CUPCAKE)
class BackUpTask extends AsyncTask<String, Void, Integer> {
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
if(ms!=null) {
ms.onPepare();
}
}
@Override
protected Integer doInBackground(String... params) {
// TODO Auto-generated method stub
// 默认路径 /data/data/(包名)/databases/*.db
File dbFile = context.getDatabasePath(DB_PATH);
File exportDir = new File(DB_BACKUP_PATH);
int result = 0;
if (!exportDir.exists()) {
exportDir.mkdirs();
}
File backup = new File(exportDir, dbFile.getName());
try {
backup.createNewFile();
fileCopy(dbFile, backup);
result = BACKUP_OK;
} catch (Exception e) {
Log.e("backup_error", e.getMessage());
result = BACKUP_FAIL;
}
return result;
}
@Override
protected void onPostExecute(Integer result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
switch (result) {
case BACKUP_OK:
if(ms!=null) {
ms.onSuccess();
}
break;
case BACKUP_FAIL:
if(ms!=null) {
ms.onFail();
}
break;
}
}
}
/**
* @Title: fileCopy
* @Description: TODO(文件拷贝方法)
* @param @param dbFile
* @param @param backup
* @param @throws IOException 设定文件
* @return void 返回类型
* @author fenglei015@deppon.com/273219
* @throws
*/
private void fileCopy(File dbFile, File backup) throws IOException {
// TODO Auto-generated method stub
FileChannel inChannel = new FileInputStream(dbFile).getChannel();
FileChannel outChannel = new FileOutputStream(backup).getChannel();
try {
inChannel.transferTo(0, inChannel.size(), outChannel);
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e("fileCopy_error", e.getMessage());
} finally {
if (inChannel != null) {
inChannel.close();
}
if (outChannel != null) {
outChannel.close();
}
}
}
}
调用DataBaseUtils中的doDataBackUp(String DB_PATH, String DB_BACKUP_PATH,MessageShow ms) 方法
public class MainActivity extends Activity {
private String DB_BACKUP_PATH=Environment.getExternalStorageDirectory()+"/feng_contactsBackup";
private String DATABASE_PATH=Environment.getExternalStorageDirectory() + "/ttttest.db";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DatabaseUtils bru=new DatabaseUtils(MainActivity.this);
bru.doDataBackUp(DB_PATH, DB_BACKUP_PATH,new MessageShow() {
@Override
public void onSuccess() {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this,"success", Toast.LENGTH_LONG).show();
}
@Override
public void onPepare() {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this,"Pepare", Toast.LENGTH_LONG).show();
}
@Override
public void onFail() {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this,"Fail", Toast.LENGTH_LONG).show();
}
});
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("DB_ERROR", "Get bean dao error cause by " + e.getMessage());
}
}
}
对整个异步回调使用的理解:
1.class A,class B
2.class A实现接口callback
3.class B拥有一个参数为callback接口类型的函数bMethod(callback cb)
4.class A运行时调用class B中的函数bMethod(callback cb),以自身传入参数
5.class B已取得A,就可以随时回调A所实现的callback接口中的方法