如何从Android

时间:2016-01-28 09:29:12

标签: android sharedpreferences

我正在尝试保存并从 sharedpreference 中获取价值,但就存储数据而言,它涉及到其工作的好处,但在检索值时我却无能为力。

//设置常量

private static final String WALLPAPER_PREF_FILE = "wallpaper";

//存储键值对的方法。

public void saveWallpaper(Context context,int wallpaperId) {

       SharedPreferences pref = context.getSharedPreferences(WALLPAPER_PREF_FILE, Activity.MODE_PRIVATE);
        SharedPreferences.Editor  editor = pref.edit();
        editor.putInt("wallpaperId", wallpaperId);
        editor.commit();
     }

//检索存储在sharedPreference中的值的方法。

 public static int getSavedWallpaper(Context context) {

        SharedPreferences pref = context.getSharedPreferences(
                WALLPAPER_PREF_FILE, Activity.MODE_PRIVATE);
        return pref.getInt("wallpaperId", -1);
    }

5 个答案:

答案 0 :(得分:4)

看起来你在检索中有拼写错误 - wallpaerId。修复它,它应该工作。 (更好的是,在保存和检索中使用常量字符串以避免这个问题)。

答案 1 :(得分:0)

尝试使用这些来接收值。

public static String getSharedPrefData(Context activity, String key) {
    SharedPreferences prefs = activity.getSharedPreferences(PREFRENCE_NAME,
            Context.MODE_PRIVATE);
    String value = "";
    if (prefs != null && prefs.contains(key)) {
        value = prefs.getInt(key, defaultValue);
    }
    return value;
}

答案 2 :(得分:0)

public class TestActivity extends AppCompatActivity {
    private SharedPreferences mSharedPreferences;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
        mSharedPreferences = getSharedPreferences("wom_user_preference",Context.MODE_PRIVATE);

        //save actual drawable id in this way.
        int resId1 =  R.drawable.splash_img;       
        saveInt("wallpaerId", resId1);

        int resId = R.drawable.fab_selector;
        int id = loadInt("wallpaerId", resId);
        Log.d("Test", "" + id);

    }

    public void saveInt(String key, int value)
    {
        if(mSharedPreferences==null)
            return;

        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putInt(key, value);
        editor.commit();
    }
    private int loadInt(String key, int defaultValue) {
        int data = mSharedPreferences.getInt(key, defaultValue);
        return data;
    }

}

答案 3 :(得分:0)

步骤1:创建名为SessionManager的公共类

public class SessionManager {

  final static String appPrefNames="MyAppInfo";
    public static SharedPreferences myPrefsSession;
    public static SharedPreferences.Editor prefsEditorSession;

    public static void setUserId(Context context, String userId) {

        myPrefsSession = context.getSharedPreferences(appPrefNames, Context.MODE_PRIVATE);
        prefsEditorSesstion = myPrefsSession.edit();
        try {
           prefsEditorSesstion.putString("user_id", userId);

        } catch (Exception e) {
            e.printStackTrace();
        }
        prefsEditorSesstion.commit();
    }


    public static String getUserId(Context c) {
        myPrefsSession = c.getSharedPreferences(appPrefNames, Context.MODE_PRIVATE);
        String userId= myPrefsSession.getString("user_id", "");
        return userId;
    }
}

第2步:在您的活动课程中

SessionManager sessionManager = new SessionManager();
//Setting UserId by method
sessionManager.setUserId(this,"12");

// get Your user id
String userId = sessionManager.getUserId();

答案 4 :(得分:-1)

private int userId;
private static final int PUSH_USER_ID = 0;

Getter Setter方法

public void setUserId(int userId) {
    this.userId = userId;
    SharedPreferences.Editor Editor = pref.edit();
    Editor.putInt(PUSH_USER_ID, userId);
    Editor.apply();
    Log.e(TAG, userId + " successfull");
}

public int getUserId() {
    return userId;
}

//重新转换int值

SharedPreferences pref = new SharedPreferences();
    pref.getUserId()