zoukankan      html  css  js  c++  java
  • getSharedPreferences 与 getPreferences 与getDefaultSharedPreferences的区别

    getSharedPreferences 与 getPreferences 的区别。

    getSharedPreferences   是Context类中的方法, 可以指定file name 以及 mode。

    getPreferences  是Activity类中的方法,只需指定mode

    而 getSharedPreferences 与getDefaultSharedPreferences的区别

    自定义的一些偏好设置用getSharedPreferences来获取,例如定义一个loginpref.xml的偏好文件

    使用

    SharedPreferences sharedPreferences = getSharedPreferences("loginpref", Context.MODE_PRIVATE);

    System.out.println("set_location = "+sharedPreferences.getBoolean("islogin", false));

    Editor editor =  sharedPreferences.edit();

    editor.putBoolean("islogin", false);

    editor.commit();

    来获取或者更改

    每个应用有一个默认的偏好文件preferences.xml,使用getDefaultSharedPreferences获取

    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);

    System.out.println("set_location = "+preferences.getBoolean("if_set_location", false));

    Editor editor =  preferences.edit();

    editor.putBoolean("if_set_location", false);

    editor.commit();

    "if_set_location"可能对应的是CheckBoxPreference或者其它,但是通用get***来获取值

    <?xml version="1.0" encoding="utf-8"?>

    <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

        <PreferenceCategory android:title="位置信息设置">

        <CheckBoxPreference

        android:key="set_location"

            android:title="打开或关闭位置信息"

            android:summary="更改您的位置设置,打开或者关闭位置共享"

            android:defaultValue="true"

        />

        <Preference

        android:key="set_sys_location"

            android:title="系统位置功能设置"

            android:summary="点击到系统设置页面打开或关闭GPS位置功能"

        /> 

        </PreferenceCategory>

    </PreferenceScreen>

  • 相关阅读:
    [TJOI2019]大中锋的游乐场——最短路+DP
    [TJOI2019]甲苯先生的滚榜——非旋转treap
    [TJOI2019]甲苯先生的字符串——矩阵乘法+递推
    [TJOI2019]唱、跳、rap和篮球——NTT+生成函数+容斥
    [ZJOI2020]字符串
    Ubuntu 20.04 工作区小记
    2020省选犯傻记
    寒假到省选的一些笔记
    AtCoder tokiomarine2020 题解
    [CF1336E]Chiori and Doll Picking
  • 原文地址:https://www.cnblogs.com/carbs/p/2602153.html
Copyright © 2011-2022 走看看