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

来源:互联网 发布:生成全排列的递归算法 编辑:程序博客网 时间:2024/04/30 23:57

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

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

[java] view plaincopy
  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 plaincopy
  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 plaincopy
  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 plaincopy
  1. MODE_WORLD_READABLE     File creation mode: allow all other applications to have read access to the created file.  

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

[html] view plaincopy
  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):

 

 

 

 

 

try{ Context context = createPackageContext("cn.test.demo", Context.CONTEXT_IGNORE_SECURITY);

[html] view plaincopy
  1. } catch (NameNotFoundException e)  
[html] view plaincopy
  1. {        e.printStackTrace();}  


SharedPreferences sharedPreferences = context.getSharedPreferences("demo", Context.MODE_WORLD_READABLE); //name 是里面存储的属性名,即:键,后面的参数是如果从sharePreferece里面取不到值,或者不存在该值的时候,获取的默认值。在这里设置为空。
String name = sharedPreferences.getString("name", ""); int age = sharedPreferences.getInt("age", 0);


CreatePackageContext这个方法有两个参数:
1.packageName 包名,要得到Context的包名
2.flags 标志位,有CONTEXT_INCLUDE_CODE和CONTEXT_IGNORE_SECURITY两个选项。CONTEXT_INCLUDE_CODE的意思是包括代码,也就是说可以执行这个包里面的代码。CONTEXT_IGNORE_SECURITY的意思是忽略安全警告,如果不加这个标志的话,有些功能是用不了的,会出现安全警告。

CreatePackageContext方法在找不到包名的时候会报NameNotFoundException异常,所以我们要捕获它。





原文链接:http://blog.csdn.net/aomandeshangxiao/article/details/6830481
0 0
原创粉丝点击