English 中文(简体)
2. 如何阅读/选择一种菜豆
原标题:How to read/write a boolean preference

Edit:该法典实际上行之有效。 我在使用该守则的法典中存在问题。 如果有任何人认为这样做是有用的。

I have a class with two methods to write and read a boolean persisted preference. However, if I write a new value and then try to read it, I still get the old value. Only if I kill the app and relaunch it, I do get the new value. Any idea what the problem is?

 Context mContext;

 ....
 public void writeFlag(boolean flag) {
        SharedPreferences sharedPreferences = PreferenceManager
                .getDefaultSharedPreferences(mContext);
        Editor editor = sharedPreferences.edit();
        editor.putBoolean("mykey", flag);
        editor.commit();
}

public boolean readFlag() {
        SharedPreferences sharedPreferences =
               PreferenceManager.getDefaultSharedPreferences(mContext);
        return sharedPreferences.getBoolean("mykey", false);
}
最佳回答
public static boolean getBooleanFromSP(String key) {
// TODO Auto-generated method stub
    SharedPreferences preferences = getApplicationContext().getSharedPreferences(" SHARED_PREFERENCES_NAME ", android.content.Context.MODE_PRIVATE);
    return preferences.getBoolean(key, false);
}//getPWDFromSP()


public static void saveBooleanInSP(String key, boolean value){
    SharedPreferences preferences = getApplicationContext().getSharedPreferences(" SHARED_PREFERENCES_NAME ", android.content.Context.MODE_PRIVATE);
    SharedPreferences.Editor editor =      preferences.edit();
    editor.putBoolean(key, value);
    editor.commit();
}//savePWDInSP()
问题回答
public static boolean getBooleanFromSP(String key) {
// TODO Auto-generated method stub
SharedPreferences preferences = 
getApplicationContext().getSharedPreferences(" SHARED_PREFERENCES_NAME ", 
  android.content.Context.MODE_PRIVATE);
 return preferences.getBoolean(key, false);
  }//getPWDFromSP()


 public static void saveBooleanInSP(String key, boolean value){
SharedPreferences preferences = getApplicationContext().getSharedPreferences(" SHARED_PREFERENCES_NAME ", android.content.Context.MODE_PRIVATE);
SharedPreferences.Editor editor =      preferences.edit();
editor.putBoolean(key, value);
editor.commit();
 }//savePWDInSP()
public class SharePref {
private static final String TAG = SharePref.class.getSimpleName();
private static SharePref mThis;
private Context mContext;
private SharedPreferences mPreference;

private SharePref() {

}

public static void init(Context context) {
    if (mThis == null) {
        mThis = new SharePref();
        mThis.setData(context);
    }
}

private void setData(Context context) {
    mThis.mContext = context;
    mPreference = mContext.getSharedPreferences(TAG, Context.MODE_PRIVATE);
}

public static SharePref getInstance() {
    return mThis;
}

public void writeString(String tag, String data) {
    SharedPreferences.Editor editor = mPreference.edit();
    editor.putString(tag, data).apply();
}

public String readString(String tag) {
    if (mPreference == null)
        mPreference = mContext.getSharedPreferences(TAG, Context.MODE_PRIVATE);
    return mPreference.getString(tag, null);
}

public void writeBoolean(String tag, boolean data) {
    SharedPreferences.Editor editor = mPreference.edit();
    editor.putBoolean(tag, data).apply();
}

public boolean readBoolean(String tag) {
    return mPreference.getBoolean(tag, false);
}


public void clear(String tag) {
    mPreference.edit().remove(tag).apply();
}


}                                                                              

/ 活动

 SharePref.init(this);       
 SharePref.getInstance().writeString("key","value");                                                                                    




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签