1
SharedPreferences的替換文件後,我可以不加載新的SharedPreferences文件。這是重新安裝應用程序後的工作。重新啓動應用程序後,沒有任何更改。無法導入SharedPreferences
這是導入/導出類:
public class Prefs {
private String PrefsDir = "/data/%packagename%/shared_prefs/";
private String ExportDir = "/";
private SharedPreferences _sharedPrefs;
private SharedPreferences.Editor _prefsEditor;
private Context context;
Prefs(Context context, String form) {
this.ExportDir = Environment.getExternalStorageDirectory().getPath() + ExportDir;
this.PrefsDir = Environment.getDataDirectory().getPath() + PrefsDir;
_sharedPrefs = context.getSharedPreferences(form, Activity.MODE_PRIVATE);
_prefsEditor = _sharedPrefs.edit();
}
private boolean copyfile(String srFile, String dtFile){
Popup.log("srFile = "+srFile);
Popup.log("dtFile = "+dtFile);
try{
File f1 = new File(srFile);
File f2 = new File(dtFile);
InputStream in = new FileInputStream(f1);
OutputStream out = new FileOutputStream(f2);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0){
out.write(buf, 0, len);
}
in.close();
out.close();
} catch(FileNotFoundException ex) {
Popup.popup(context, "File not found");
Popup.log(ex.getMessage());
return false;
} catch(IOException e) {
Popup.popup(context, "IO Error");
return false;
}
return true;
}
boolean Export(String form) {
return copyfile(PrefsDir+form+".xml", ExportDir+form+".xml");
}
boolean Import(String form) {
return copyfile(ExportDir+form+".xml", PrefsDir+form+".xml");
}
}
對不起了我的英語