读写其他应用程序的sharedpreferences
2016-06-18 09:29
399 查看
正在学习读写其他应用程序的sharedpreferences,所以把想到的东西在这儿再梳理一下吧。
首先,要读取其他应用的sharedpreferences,前提是创建该sharedpreferences的应用程序指定该访问权限是 MODE_WORLD_READABLE,或者是 MODE_WORLD_WRITABLE.前者是在其他应用程序中只读,后者是在其他应用程序中可读可写。第二就是,获取其他程序的sharedpreference对应的CONTEXT,代码如下
try { Context mcontext= createPackageContext("com.example.mpreferences",CONTEXT_IGNORE_SECURITY); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); }
其中,”com.example.mpreferences”是其他应用程序的包名,包名即是一个应用程序的标识。CONTEXT_IGNORE_SECURITY是一个flag,忽略安全性。
第三,利用Context 的mcontext.getSharedPreferences(“wshuang_preference”,MODE_PRIVATE)来获取对应的sharedpreference。
第四就是正常读取了。
try { Context mcontext = createPackageContext("com.example.mpreferences", CONTEXT_IGNORE_SECURITY); SharedPreferences msharedpreferences = mcontext.getSharedPreferences("wshuang_preference", MODE_PRIVATE); int count = msharedpreferences.getInt("count", 0); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); }
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories