Android SharedPreferences使用

阅读更多
key_value存储介质,xml文档形式,存入路径data/data/{packagename}/shared_prefs下

private void dispose()
    {
        /*
         * 获取 SharedPreferences 对象
         */
        SharedPreferences preferences = getSharedPreferences(SHARED_PREFERENCES_NAME, Activity.MODE_PRIVATE);
       
        /*
         * 获取 SharedPreferences.Editor 对象
         */
        Editor editor = preferences.edit();
       
        /*
         * Editor 赋值
         */
        editor.putString("name", SHARED_PREFERENCES_NAME);
        editor.putString("hobby", "篮球、android");
       
        /*
         * Editor 游离态数据提交
         */
        editor.commit();
       
        read();
    }

    private void read()
    {
        /*
         * 获取 SharedPreferences 对象
         */
        SharedPreferences preferences = getSharedPreferences(SHARED_PREFERENCES_NAME, Activity.MODE_PRIVATE);
       
        String name = preferences.getString("name", "");
        String hobby = preferences.getString("hobby", "");
       
        Toast.makeText(DisposePreferences.this, "name value:" + name + "\n" + "hobby value:" + hobby, Toast.LENGTH_LONG).show();
    }

你可能感兴趣的:(Android SharedPreferences使用)