存:
public void putString(Context context, String key, String value) {
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_WORLD_READABLE|Context.MODE_MULTI_PROCESS);
SharedPreferences.Editor editor = settings.edit();
editor.putString(key, value);
editor.commit();
}
在android4.0之后,之前没有加入Context.MODE_MULTI_PROCESS参数,导致不能获取到修改后的数据。
取:
Context xcontext=null;
try{
xcontext = createPackageContext("com.android.settings", Context.CONTEXT_IGNORE_SECURITY);
} catch (Exception e){
}
SharedPreferences settings = xcontext.getSharedPreferences("ruixin", Context.MODE_WORLD_READABLE|
Context.MODE_MULTI_PROCESS);
String name=settings.getString("bluetoothName",null);
“com.android.settings” 是包名
“ruixin” 是PREFERENCE_NAME