应用A 存取正常,应用B 只能读取应用A中的数据,不能进行修改
存数据
public static void setProcessString(Context context, String fileName, String key, String num) {
SharedPreferences sp = context.getSharedPreferences(fileName, Context.MODE_WORLD_READABLE);SharedPreferences.Editor et = sp.edit();
et.putString(key, num);
et.commit();
}
Context.MODE_WORLD_WRITEABLE 时,会出现脏数据
Context.MODE_WORLD_READABLE
Context.MODE_MULTI_PROCESS 没有效果
读数据
public String getOtherProcessString(Context context, String packageName, String fileName, String key, String defaultVaule) {
int MODE = Context.MODE_MULTI_PROCESS;
String msg = "";
try {
Context otherAppsContext = context.createPackageContext(pageName, Context.CONTEXT_IGNORE_SECURITY);
SharedPreferences sharedPreferences = otherAppsContext.getSharedPreferences(fileName, MODE);
msg = sharedPreferences.getString(key, defaultVaule);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return msg;
}
Context.MODE_WORLD_READABLE 也可以
Context.MODE_WORLD_WRITEABLE; 也可以