티스토리 뷰

private void getPreferences() { 
        SharedPreferences pref = getSharedPreferences("pref", MODE_PRIVATE); 
        String a = pref.getString("key1", ""); 
        Toast.makeText(this, a, 0).show(); 
    } 
  
    // 값 저장하기 
    private void savePreferences() { 
        SharedPreferences pref = getSharedPreferences("pref", MODE_PRIVATE); 
        SharedPreferences.Editor editor = pref.edit(); 
        editor.putString("key1", "data"); 
        editor.commit(); 
    } 
  
    // 값(Key Data) 삭제하기 
    private void removePreferences() { 
        SharedPreferences pref = getSharedPreferences("pref", MODE_PRIVATE); 
        SharedPreferences.Editor editor = pref.edit(); 
        editor.remove("key1"); 
        editor.commit(); 
    } 
  
    // 값(ALL Data) 삭제하기 
    private void removeAllPreferences() { 
        SharedPreferences pref = getSharedPreferences("pref", MODE_PRIVATE); 
        SharedPreferences.Editor editor = pref.edit(); 
        editor.clear(); 
        editor.commit(); 
    }
Activity가 아닌 다른 class에서 사용하려면, context만 넘겨주면 된다
SharedPreferences pref = mContext.getSharedPreferences("pref", MODE_PRIVATE);
반응형
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
글 보관함