• sharePreference的几个重点


    一.  SharePreferences是用来存储一些简单配置信息的一种机制,使用Map数据结构来存储数据,以键值对的方式存储,采用了XML格式将数据存储到设备中,文件存放在/data/data/<package name>/shared_prefs目录下。

    二.getSharedPreferences(name,mode)

    方法的第一个参数用于指定该文件的名称,名称不用带后缀,后缀会由Android自动加上;

    方法的第二个参数指定文件的操作模式,共有四种操作模式。

    四种操作模式分别为:

    1. MODE_APPEND: 追加方式存储

    2. MODE_PRIVATE: 私有方式存储,其他应用无法访问

    3. MODE_WORLD_READABLE: 表示当前文件可以被其他应用读取

    4. MODE_WORLD_WRITEABLE: 表示当前文件可以被其他应用写入

    5. MODE_MULTI_PROCESS: 适用于多进程访问(目前已被废弃,google官方推荐使用ContentProvider来实现进程间共享访问)

    三,如果你想要删除通过SharedPreferences产生的文件,可以通过以下方法

    1 File file= new File("/data/data/"+getPackageName().toString()+"/shared_prefs","Activity.xml");
    2 
    3 if(file.exists()){
    4 
    5 file.delete(); 

    五,访问其他应用中的Preference

    如果要访问其他应用中的Preference,必须满足的条件是,要访问的应用的Preference创建时指定了Context.MODE_WORLD_READABLE或者Context.MODE_WORLD_WRITEABLE权限。

    举例,假如有个<package name>为com.wujay.action下面的应用使用了下面语句创建了Preference,getSharedPreferences("wujay", Context.MODE_WORLD_READABLE),

    现在要访问该Preferences:

    首先,需要创建上面的Context,然后通过Context访问Preferences,访问preference时会在应用所在包下的shared_prefs目录找到preference:

    Context otherAppsContext = createPackageContext("com.wujay.action", Context.CONTEXT_IGNORE_SECURITY);
    SharedPreferences sharedPreferences = otherAppsContext.getSharedPreferences("wujay", Context.MODE_WORLD_READABLE);
    String name = sharedPreferences.getString("name", "");
    int age = sharedPreferences.getInt("age", 0);

    如果不通过创建Context访问其他应用的preference,可以以读取xml文件方式直接访问其他应用preference对应的xml文件,如:
    File xmlFile = new File(“/data/data/<package name>/shared_prefs/itcast.xml”);//<package name>应替换成应用的包名。

    六,使用代码

    public class PreferencesUtil {  
        private Context context;  
          
        public PreferencesUtil(Context context) {  
            this.context = context;  
        }  
        /**  
         * 保存参数  
         * @param name 姓名  
         * @param age 年龄  
         */  
        public void save(String name, Integer age) {  
            SharedPreferences preferences = context.getSharedPreferences("student", Context.MODE_PRIVATE);  
            Editor editor = preferences.edit();  
            editor.putString("name", name);  
            editor.putInt("age", age);  
            editor.commit();  
        }  
        /**  
         * 获取各项配置参数  
         * @return  
         */  
        public Map<String, String> getPreferences(){  
            Map<String, String> params = new HashMap<String, String>();  
            SharedPreferences preferences = context.getSharedPreferences("student", Context.MODE_PRIVATE);  
            params.put("name", preferences.getString("name", ""));  
            params.put("age", String.valueOf(preferences.getInt("age", 0)));  
            return params;  
        }  
    }  
  • 相关阅读:
    报错:无法截断表 '某表',因为该表正由 FOREIGN KEY 约束引用
    如何选择使用结构或类
    C#交换两个变量值的多种写法
    什么是.Net, IL, CLI, BCL, FCL, CTS, CLS, CLR, JIT
    把数据库中有关枚举项值的数字字符串转换成文字字符串
    为参数类型一样返回类型不同的接口写一个泛型方法
    报错:System.NotSupportedException: LINQ to Entities does not recognize the method
    关于泛型类和扩展方法的一点思考
    在ASP.NET MVC下通过短信验证码注册
    ASP.NET MVC下实现前端视图页的Session
  • 原文地址:https://www.cnblogs.com/yoyohong/p/6858255.html
Copyright © 2020-2023  润新知