其中一个应用写入数据到SharedPreference:
SharedPreferences sharedPreferences = this.getSharedPreferences("xml_name",
Context.MODE_WORLD_READABLE | Context.MODE_MULTI_PROCESS);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("key", true);
editor.commit();
另外一个应用读取SharedPreference的数据:
try { //创建进程A的上下文
Context phoneContext = this.createPackageContext("com.android.phone",
Context.CONTEXT_IGNORE_SECURITY);
SharedPreferences sp = phoneContext.getSharedPreferences("xml_name",
Context.MODE_WORLD_READABLE | Context.MODE_MULTI_PROCESS);
int values = sp.getInt("key" , 0);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
其中需要注意的是,包名不要写错
网友评论