我可以备份并恢复我最近的数据库,但是我想保存一个带时间戳的Sqlite数据库,例如" back_up_12_11_17_13_32"。这样,用户不仅可以恢复最近的备份,还可以恢复他需要的备份。
我想知道如何解决这个问题?
答案 0 :(得分:0)
我使用以下命令创建备份名称。
private void setFullFilename() {
backupfullfilename.setText(
backupbasepart.getText().toString() +
backupdatetimepart.getText().toString() +
backupextension.getText().toString()
);
}
,其中
backupdatetimepart默认为YYMMDDhhmm格式的当前时间(活动开始时),通过以下方式获得: -
Calendar cldr = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
return "_" + sdf.format(cldr.getTime());
backupextension默认为 .bkp
注意到我允许编辑所有内容。
App的备份/恢复活动如下: -
使用此目录还允许从其他来源复制数据库,因此用户可以共享数据库,如果他们愿意,或者可以使用此功能来确定和解决问题。
还原微调器中显示的备份由3个输入决定,这些输入允许一定的灵活性 - 默认情况下,仅列出标准备份。但是,删除基本文件名将导致所有文件(所有文件扩展名为.bkp(文件扩展名仅显示具有该扩展名的文件,将其删除显示所有扩展名))。
e.g。如果所有都被清空,那么你可以拥有: -
当归结为它时,提供可恢复文件列表的核心代码是: -
private void populateRestoreSpinner(Spinner spn,
TextView tv,
String basefilename,
String fileext,
StoreData sd,
TextView restorebutton) {
int fcount = 0;
ArrayList<File> reverseflist = new ArrayList<>();
//
sd = new StoreData(getResources().getString(R.string.backupdirectoryname),"xxx",true);
sd.refreshOtherFilesInDirectory();
// Build the File ArrayList
ArrayList<File> flst = new ArrayList<>(sd.getFilesInDirectory());
// Ascertain the relevant files that are needed for the restore backup
// file selector
for(int i = 0; i < flst.size(); i++) {
boolean endingok = flst.get(i).getName().endsWith(fileext);
boolean containsok = flst.get(i).getName().contains(basefilename);
if((strictbackupmode && endingok && containsok)
|| (!strictbackupmode && (endingok || containsok))) {
fcount++;
} else {
flst.remove(i);
i--;
}
}
// Reverse the order of the list so most recent backups appear first
// Also hide/show the Restore button and spinner according to if
// files exist or not
// (doing nothing in the case where the is no restore button i.e.
// null has been passed)
if(flst.size() > 0) {
for (int i = (flst.size() -1); i >= 0; i--) {
reverseflist.add(flst.get(i));
}
if (restorebutton != null) {
spn.setVisibility(View.VISIBLE);
restorebutton.setVisibility(View.VISIBLE);
}
} else {
if (restorebutton != null) {
spn.setVisibility(View.INVISIBLE);
restorebutton.setVisibility(View.INVISIBLE);
}
}
// Set the available count for display
//String bcnt = "Available Backups=" + Integer.toString(reverseflist.size());
tv.setText(Integer.toString(reverseflist.size()));
// Set the spinner adapter and dropdown layout and then set the
// spinner's adapter
AdapterFileList afl = new AdapterFileList(this,
R.layout.filelist,
reverseflist,
getIntent());
afl.setDropDownViewResource(R.layout.filelist);
spn.setAdapter(afl);
}
请注意! StoreData是一个美化的文件/目录列表,它很长,但主要有以下成员: -
private String directory;
private String subdirectory;
private String filename;
private boolean mounted;
private boolean inerror;
private boolean fileexists;
private boolean direxists;
private long errorcode;
private ArrayList<String> errorlist = new ArrayList<>();
private ArrayList<File> otherfilesindirectory = new ArrayList<>();