2016-09-13 4 views
-2

지도를 표시하는 데 Maps API를 사용하는 아주 기본적인 응용 프로그램을 가지고 있으며, 길게 누르면 해당 위치에 마커가 내려갑니다. 다른 위치를 선택하면 현재 마커가 삭제되고 새 마커가 만들어집니다.안드로이드에 소량의 데이터를 저장하는 가장 좋은 방법

응용 프로그램이 내가 원하는 순간에 작업을 수행하지만 응용 프로그램을 닫은 후에도 데이터가 지속되도록 할 수 있습니다.

이 내가 현재 할 노력하고있어 무엇 : 응용 프로그램이

  String filename = "userLatLng"; 
      FileOutputStream outputStream; 

      try { 
       outputStream = openFileOutput(filename, Context.MODE_PRIVATE); 

       outputStream.write(Integer.parseInt(yayaParking.toString())); 
       outputStream.close(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

는 데이터를 저장하는 방법을해야 그 시작 후 데이터를 읽는 데 사용되는

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_maps); 
     // Obtain the SupportMapFragment and get notified when the map is ready to be used. 
     SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
      .findFragmentById(R.id.map); 
     mapFragment.getMapAsync(this); 

     BufferedReader reader; 

     try { 
      final InputStream file = getAssets().open("userLatLng"); 
      reader = new BufferedReader(new InputStreamReader(file)); 
      String line = reader.readLine(); 
      line = reader.readLine(); 
      System.out.print("Getting Data"); 
     } catch (IOException ioe) { 
      ioe.printStackTrace(); 
     } 
    } 

. 나는이 일을 아직하지 못했기 때문에 어떤 도움을 주시면 감사하겠습니다!

+3

공유 환경 설정에 저장 https://developer.android.com/training/basics/data-storage/shared-preferences.html –

답변

0

사용 SharedPreference API :

SharedPreferences preferences; 

는 다음과 같이 데이터를 저장합니다.

SharedPreferences sharedPref = context.getSharedPreferences(
     "lat_pref", Context.MODE_PRIVATE); 

    SharedPreferences.Editor editor = sharedPref.edit(); 
    editor.putString(KEY_LAT, "12.7333"); // your lat and lng 
    editor.putString(KEY_LNG, "12.7333"); 
    editor.commit(); 

당신은 당신이 어떤 일정 파일에 KEY_LAT & KEY_LNG를 선언 할 수

SharedPreferences sharedPref = context.getSharedPreferences("lat_pref",Context.MODE_PRIVATE); 

String slat = sharedPref.getString(KEY_LAT, "0.0"); //0.0 is default value, you can change it to any value you like. This default value is applied when the key is not present in SharedPrefernce 

String slng = sharedPref.getString(KEY_LNG, "0.0"); 

//Convert String Lat and Lng to double values 
double lat = Double.parseDouble(slat); 
double lng = Double.parseDouble(slng); 

아래처럼 검색 할 수 있습니다. 당신이 활동에있는 경우 getActivity()

편집

editor.putLong(KEY_LAT, Double.doubleToLongBits(location.getLatitude())); 

를 사용하여 조각의 경우

public static final String KEY_LAT = "latitude"; 
public static final String KEY_LNG = "longitude"; 

다음과 같은를 검색, 문맥으로 this를 사용

double lat = Double.longBitsToDouble(sharedPref.getLong(KEY_LAT, 0); 

마 Longitu에 대해서도 마찬가지다. de

+0

분명히 사람들은 이것이 바보 같은 질문이라고 생각하지만'preference_file_key 'putDouble'도 마찬가지입니다 (둘 다 빨간색으로 표시됩니다). 죄송하지만 안드로이드에 아주 익숙합니다! –

+0

@EladKarni 수정 된 답변을 확인하십시오. – Aniruddha

+0

감사합니다. 'setOnMapLongClickListener'를 사용할 때 정확한 컨텍스트를 참조 할 수 없기 때문에 범위에 오류가 발생한다고 생각합니다. 어떤 아이디어? 문맥으로'this '를 시도했지만 작동하지 않습니다. ( –

0

sharedPreferences에 저장할 수 있습니다.

public String getLocation() { 
     SharedPreferences pref; = PreferenceManager.getDefaultSharedPreferences(getBaseContext()); 
     return pref.getString("StoredLocation", ""); 
    } 

    public void setLocation(String value) { 
     SharedPreferences pref; = PreferenceManager.getDefaultSharedPreferences(getBaseContext()); 
     Editor editor = pref.edit(); 
     editor.putString("StoredLocation", value); 
     editor.commit(); 
    } 

위 코드를 사용하여 위치를 저장하고 검색 할 수 있습니다.

0

SharedPreference의 전역 변수를 만듭니다. 작은 데이터 저장을위한

private void savePreference(String your_data){ 

    preferences = getSharedPreferences("name_ur_preference", MODE_PRIVATE); 
    SharedPreferences.Editor editor = preferences.edit(); 

    editor.putString("DATA", your_data); 

    editor.apply(); 
} 
관련 문제