SharedPreferences具体使用方法及createPackageContext方法(获取其他应用的共享文件)

很多时候我们开发的软件需要向用户提供软件参数设置功能,Android应用,我们最适合采用什么方式保存软件配置参数呢?在Android平台上,提供了一个SharedPreferences类,它是一个轻量级的存储类,特别适合用于保存软件配置参数。使用SharedPreferences保存数据,其背后是用xml文件存放数据,使用简易的键值对存储。文件存放在/data/data/<packagename>/shared_prefs目录下

下面我们给点代码,说下简单的使用:

[java]  view plain copy
  1. //文件命名为demo,私有模式  
  2. SharedPreferences sharedPreferences = getSharedPreferences("demo", Context.MODE_PRIVATE);  
  3. Editor editor = sharedPreferences.edit();//获取编辑器  
  4. editor.putString("name""hello");  
  5. editor.putInt("age"6);  
  6. editor.commit();//提交修改  
这样,我们就把信息存储到了 /data/data/<packagename>/shared_prefs/demo.xml文件里面,现在你打开这个文件,就可以看到内容已经被存储。如下:

[html]  view plain copy
  1. <?xml version='1.0' encoding='utf-8' standalone='yes' ?>  
  2. <map>  
  3. <string name="name">hello</string>  
  4. <int name="age" value="6" />  
  5. </map>  
getSharedPreferences(name,mode)方法的第一个参数用于指定该文件的名称,最好定义为一个静态字符串,另外,名称如上面所示,不用带后缀名,后缀名会由系统自动加上。方法的第二个参数指定文件的操作模式,共有四种操作模式,这四种模式想必大家都有一定的了解。这里简单说一下:

MODE_APPEND File creation mode: for use with openFileOutput(String, int), if the file already exists then write data to the end of the existing file instead of erasing it.    如果文件已经存在,在文件内容后面添加

[html]  view plain copy
  1. MODE_PRIVATE File creation mode: the default mode, where the created file can only be accessed by the calling application (or all applications sharing the same user ID).  
设置为默认模式,在创建的文件只能该应用能够使用(或所有的应用程序共享同一个用户标识号)

[html]  view plain copy
  1. MODE_WORLD_READABLE     File creation mode: allow all other applications to have read access to the created file.  

允许其他应用读该应用创建的文件。

[html]  view plain copy
  1. MODE_WORLD_WRITEABLE    File creation mode: allow all other applications to have write access to the created file.  
允许其他应用写该应用创建的文件。

所以,如果你希望SharedPreferences背后使用的xml文件能被其他应用读和写,可以指定Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE权限。
另外Activity还提供了另一个getPreferences(mode)方法操作SharedPreferences,这个方法默认使用当前类不带包名的类名作为文件的名称。

如果我们的模式设置为Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE权限,我们其他的应用是可以访问的,下面是其他应用访问的代码(假如上面代码的包名为cn.test.demo):

[html]  view plain copy
  1. try{  
  2.    Context context = createPackageContext("cn.test.demo", Context.CONTEXT_IGNORE_SECURITY);  
  3. <pre name="code" class="html">} catch (NameNotFoundException e)</pre><pre name="code" class="html">{        e.printStackTrace();}</pre><br>SharedPreferences sharedPreferences = context.getSharedPreferences("demo", Context.MODE_WORLD_READABLE); //name 是里面存储的属性名,即:键,后面的参数是如果从sharePreferece里面取不到值,或者不存在该值的时候,获取的默认值。在这里设置为空。<span style="color:#000000;widows:2; text-transform:none; text-indent:0px; border-collapse:separate; font:medium simsun; white-space:normal; orphans:2; letter-spacing:normal; word-spacing:0px"></span><br>String name = sharedPreferences.getString("name", ""); int age = sharedPreferences.getInt("age", 0);<pre></pre><p></p><pre></pre><br>CreatePackageContext这个方法有两个参数:<br>1.packageName 包名,要得到Context的包名<br>2.flags 标志位,有CONTEXT_INCLUDE_CODE和CONTEXT_IGNORE_SECURITY两个选项。CONTEXT_INCLUDE_CODE的意思是包括代码,也就是说可以执行这个包里面的代码。CONTEXT_IGNORE_SECURITY的意思是忽略安全警告,如果不加这个标志的话,有些功能是用不了的,会出现安全警告。<p></p><span style="color:#000000;widows:2; text-transform:none; text-indent:0px; border-collapse:separate; font:medium simsun; white-space:normal; orphans:2; letter-spacing:normal; word-spacing:0px">CreatePackageContext方法在找不到包名的时候会报NameNotFoundException异常,所以我们要捕获它。</span><br>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值