2013-07-14 9 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

물론 그들이 저장할 수있는 변수. 첫 번째 루틴은 공유 환경 설정을 초기화하는 것입니다 (공유 환경 설정을로드하기 위해 코드에서 값을 초기화해야 저장되므로 코드에서이 값을 초기화해야합니다). 두 번째 루틴은 공유 환경 설정을 저장하는 것입니다. 세 번째 루틴은 Shared Preferences를로드하는 것입니다.

이 여기에 일부

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);    
} 
관련 문제