进程A(写入sp):
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();
进程B(读取sp):
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();
}
需要导入的包
import android.content.pm.PackageManager;
import android.content.SharedPreferences;