Android源码开发,经常会遇到sharedpreference在一个模块写,在另一个模块读的情况,实现起来也很简单
在Dialer模块里写
private static final String PREFER_NAME = "share";
private static int MODE = Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE + Context.MODE_MULTI_PROCESS;
SharedPreferences share = getSharedPreferences(PREFER_NAME,MODE);
share.putBoolean("vibratecall", true);
share.commit();
在Settings模块里读
private Context getTargetContext() throws NameNotFoundException {
return getContext().createPackageContext("com.android.dialer", Context.CONTEXT_IGNORE_SECURITY);
}
try {
SharedPreferences share = getTargetContext().getSharedPreferences(PREFER_NAME,MODE);
boolean needVibrate = share.getBoolean(