欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android SharedPreferences使用

程序员文章站 2022-05-16 17:29:58
...
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();
    }
相关标签: SharedPreferences