2013-07-14 38 views
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"); 
    } 
} 

對不起了我的英語

回答

0

我實施的第一部分包括定義用於檢索共享偏好的按鍵和聲明當然他們可以存儲到變量。 第一個例程是初始化共享偏好(以加載共享偏好,U必須的代碼值來保存這樣u需要一些代碼中的代碼來初始化這些值)。 第二個例程是保存共享首選項。 第三個例程是加載共享首選項。

這是關鍵的定義部分

private SharedPreferences sharedPreferences; 
public final static String MARV_INIT = "MARV_INIT"; 
public final static String MARV_LAT = "MARV_LATITUDE"; 
public final static String MARV_LON = "MARV_LONGITUDE"; 
public final static String MARV_ZOOM = "MARV_ZOOM"; 
public final static String MARV_PDFL = "MARV_PDFL"; 
public final static String MARV_PRON = "MARV_PRON"; 
public final static String MARV_LTRD = "MARV_LTRD"; 

這裏是3個例程。

// Shared Preferences and other data loading routines for this activity 
private void initSharedPreferences() { 
    Log.i(TAG, "Initializing shared preferences ..."); 
    Editor editor = sharedPreferences.edit(); 

    editor.putInt(MARV_INIT, 1); 
    editor.putString(MARV_LAT, ((Double) SG_LATITUDE).toString()); 
    editor.putString(MARV_LON, ((Double) SG_LONGITUDE).toString()); 
    editor.putFloat(MARV_ZOOM, (float) DEFAULT_ZOOM); 
    editor.putString(MARV_PDFL, getString(R.string.default_poi_list)); 
    editor.putBoolean(MARV_PRON, true); 
    editor.putFloat(MARV_LTRD, (float) LocationAlertService.DEFAULT_RADIUS); 

    editor.commit(); 
} 

private void saveSharedPreferences() { 

    // Update global variables first 
    updateCameraProperties(); 

    // Now save global variables into shared preferences 
    Editor editor = sharedPreferences.edit(); 

    editor.putString(MARV_LAT, 
      ((Double) lastCameraPosition.latitude).toString()); 
    editor.putString(MARV_LON, 
      ((Double) lastCameraPosition.longitude).toString()); 
    editor.putFloat(MARV_ZOOM, lastCameraZoom); 
    editor.putString(MARV_PDFL, poiDocFileListUrl); 
    editor.putBoolean(MARV_PRON, proximityAlertsOn); 
    editor.putFloat(MARV_LTRD, lastRadiusUsed); 

    editor.commit(); 
} 

private void loadSharedPreferences() { 

    lastCameraPosition = new LatLng(Double.parseDouble(sharedPreferences 
      .getString(MARV_LAT, ((Double) SG_LATITUDE).toString())), 
      Double.parseDouble(sharedPreferences.getString(MARV_LON, 
        ((Double) SG_LONGITUDE).toString()))); 
    lastCameraZoom = sharedPreferences.getFloat(MARV_ZOOM, DEFAULT_ZOOM); 
    poiDocFileListUrl = sharedPreferences.getString(MARV_PDFL, 
      getString(R.string.default_poi_list)); 
    proximityAlertsOn = sharedPreferences.getBoolean(MARV_PRON, true); 
    lastRadiusUsed = sharedPreferences.getFloat(MARV_LTRD, LocationAlertService.DEFAULT_RADIUS);    
}