中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

Android數(shù)據(jù)庫(kù)備份類(lèi)

2018-07-20    來(lái)源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬(wàn)Linux鏡像隨意使用
/**
 * Android數(shù)據(jù)庫(kù)備份類(lèi)
 */
public class BackupTask extends AsyncTask<String, Void, Integer> implements
        CompletionListener {
 
    // 定義常量
    public static final int BACKUP_SUCCESS = 1;
    public static final int RESTORE_SUCCESS = 2;
    public static final int BACKUP_ERROR = 3;
    public static final int RESTORE_NOFLEERROR = 4;
    public static final String COMMAND_BACKUP = "backupDatabase";
    public static final String COMMAND_RESTORE = "restroeDatabase";
    private Context mContext;
 
    public BackupTask(Context context) {
        this.mContext = context;
    }
 
    @Override
    protected Integer doInBackground(String... params) {
        // 1,獲得數(shù)據(jù)庫(kù)路徑
        File dbFile = mContext.getDatabasePath("xxx.db");
        // 2,創(chuàng)建保存的數(shù)據(jù)庫(kù)的路徑
        File exportDir = new File(Environment.getExternalStorageDirectory(),
                "shopBackup");
        if (!exportDir.exists()) {
            exportDir.mkdirs();
        }
        File backup = new File(exportDir, dbFile.getName());
        // 3,檢查操作
        String command = params[0];
        if (command.equals(COMMAND_BACKUP)) {
            // 復(fù)制文件
            try {
                backup.createNewFile();
                fileCopy(dbFile, backup);
                return BACKUP_SUCCESS;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return BACKUP_ERROR;
            }
        } else {
            return BACKUP_ERROR;
        }
 
    }
 
    private void fileCopy(File source, File dest) throws IOException {
        FileChannel inChannel = new FileInputStream(source).getChannel();
        FileChannel outChannel = new FileOutputStream(dest).getChannel();
        // FileInputStream fis = new FileInputStream(dbFile);
        // FileOutputStream fos = new FileOutputStream(backup);
        // byte buffer[] = new byte[4 * 1024];
        // while(fis.read(buffer) != -1){
        // fos.write(buffer);
        // }
        // fos.flush();
        //
        long size = inChannel.size();
        try {
            inChannel.transferTo(0, inChannel.size(), outChannel);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (inChannel != null) {
                inChannel.close();
            }
            if (outChannel != null) {
                outChannel.close();
            }
        }
    }
 
    @Override
    protected void onPostExecute(Integer result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        switch (result) {
        case BACKUP_SUCCESS:
            onBackupComplete();
            break;
 
        default:
            break;
        }
    }
 
    @Override
    public void onBackupComplete() {
 
        Log.d("backup", "ok");
 
    }
 
    @Override
    public void onRestoreComplete() {
        // TODO Auto-generated method stub
 
    }
 
    @Override
    public void onError(int errorCode) {
        // TODO Auto-generated method stub
 
    }
 
}

標(biāo)簽: 數(shù)據(jù)庫(kù)

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇: Android Launcher3去掉所有應(yīng)用列表,橫屏?xí)r左右兩側(cè)的留空

下一篇: Android獲取Build和內(nèi)存,分辨率等信息