读写其他应用程序的SharedPreference

2013-12-28 18:03:40 

要读写其他应用的SharedPreference,前提是创建该SharedPreference的程序指定相应的可读或可写的权限, 如下:

 1     private void accessSharedPreference() {
 2         Context useCount = null;
 3         try {
 4             // 获取其他程序所对应的Context
 5             useCount = createPackageContext("org.crazyit.io",
 6                     Context.CONTEXT_IGNORE_SECURITY);
 7         } catch (NameNotFoundException e) {
 8             e.printStackTrace();
 9         }
10         // 使用其他程序的Context获取对应的SharedPreferences
11         SharedPreferences prefs = useCount.getSharedPreferences("count",
12                 Context.MODE_WORLD_READABLE);
13         // 读取数据
14         int count = prefs.getInt("count", 0);
15         TextView show = (TextView) findViewById(R.id.show);
16         // 显示读取的数据内容
17         show.setText("UseCount应用程序以前被使用了" + count + "次。");
18     }

这里简单记录,更详细的大家用度娘吧...

原文地址:https://www.cnblogs.com/wlrhnh/p/3495616.html