2010-10-31 35 views
9

Là người mới trong thế giới Android và thăng tiến với niềm vui mỗi ngày;) Tôi muốn chia sẻ các ví dụ về cách sử dụng phổ biến.Cách sử dụng SharedPreferences như LocalStore, theo cách tổng quát hơn?

Đây là ví dụ về cách sử dụng SharedPreferences với lớp LocalStore chung.

tạo một lớp chung được sử dụng bởi hoạt động chính của bạn hoặc theo bất kỳ hoạt động phụ nào.

public class LocalStore { 

     private static final String TAG = "LocalStore"; 
     private static final String PREF_FILE_NAME = "userprefs"; 

     public static void clear(Context context) { 
      clear(context, "unknown"); 
     } 
     public static void clear(Context context, String caller) { 
      Editor editor = 
       context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE).edit(); 
      editor.clear(); 
      editor.commit(); 
      Log.d(TAG, "caller:"+caller + "|clear LocalStore"); 
     } 

     public static boolean setCustomBooleanData(String key, boolean value, Context context) { 
     Editor editor = 
      context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE).edit(); 
     editor.putBoolean(key, value); 

     return editor.commit(); 
    } 
    public static boolean getCustomBooleanData(String key, Context context) { 
     SharedPreferences savedSession = 
      context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE); 

     return (savedSession.getBoolean(key, false)); 
    } 

    public static boolean setCustomStringData(String key, String value, Context context) { 
     Editor editor = 
      context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE).edit(); 
     editor.putString(key, value); 

     return editor.commit(); 
    } 
    public static String getCustomStringData(String key, Context context) { 
     SharedPreferences savedSession = 
      context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE); 

     return (savedSession.getString(key, null)); 
    } 


    public static boolean isCustomStringExistInLocal(String customKey, Context context) { 
     SharedPreferences savedSession = 
      context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE); 

     return (savedSession.getString(customKey, null))==null?false:true; 
    } 

public static boolean saveObject(String objKey, Serializable dataObj, Context context) { 
     Editor editor = 
      context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE).edit(); 
     editor.putString(objKey, ObjectSerializer.serialize(dataObj)); 

     Log.d(TAG, "savedObject| objKey:"+objKey+"/" + dataObj.toString()); 

     return editor.commit(); 
    } 
    public static Object getObject(String objKey, Context context) { 
     SharedPreferences savedSession = 
      context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE); 

     Object dataObj = ObjectSerializer.deserialize(savedSession.getString(objKey, null)); 

     return dataObj; 
    } 

    } 

Lưu ý: Bạn có thể sử dụng ObjectSerializer từ here

Thưởng thức!

cập nhật bổ sung: tôi thực hiện một thư viện để sử dụng MEMDISKCACHE và SHAREDPREF như GENERIC_STORE bất cứ ai quan tâm có thể sử dụng nó từ
->https://github.com/wareninja/generic-store-for-android

+13

StackOverflow FAQ nói: "Nó cũng hoàn toàn tốt đẹp để hỏi và trả lời câu hỏi của riêng bạn, miễn là bạn giả vờ bạn đang gặp nguy hiểm: cụm từ nó dưới dạng một câu hỏi. " –

+0

Bạn có thể làm cho câu hỏi và câu trả lời phù hợp này thay vì mọi thứ trong nội dung câu hỏi hay không. Nếu không, nó có thể bị xóa. – Kev

+2

Thật tuyệt khi bạn chia sẻ :), nhưng tôi nghĩ điều này thuộc về một blog chứ không phải là trang web câu hỏi và trả lời. –

Trả lời

2

Giả sử bạn muốn có một số lời khuyên về cách để cải thiện nó hơn nữa, ở đây bạn đi.

  • Thông thường Android tuân theo quy ước để giữ biến Context làm thông số đầu tiên. Đó là thực hành tốt để làm điều này khi tạo bất kỳ thư viện chung nào.
  • Nếu bạn muốn làm cho nó chung chung hơn, tại sao không thử quá tải phương thức? Nó sẽ giúp nhà phát triển linh hoạt hơn khi đặt giá trị giống như cách tính năng bổ sung được xử lý trong lớp Intent.

Ví dụ:

public static boolean setData(Context, String key, boolean value) { 
     Editor editor = context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE).edit(); 
     editor.putBoolean(key, value); 
     return editor.commit(); 
    } 
public static boolean setData(Context, String key, String value) { 
     Editor editor = context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE).edit(); 
     editor.putString(key, value); 
     return editor.commit(); 
    } 

Vì vậy, bạn chỉ có thể gọi các chức năng quá tải như thế này:

setData(this, "myBoolean", true); 
setData(this, "myString", "Its Awesome"); 
+1

Cảm ơn! Câu trả lời tốt – user1755546

Các vấn đề liên quan