SharedPreferences保存取值不可取的坑

来源:互联网 发布:数据维护是什么意思 编辑:程序博客网 时间:2024/05/22 15:41

跨进程使用SharedPreferences的使用,需要使用MODE_MULTI_PROCESS模式,代码如下:


  1. public void writeSharedprefs(int pos) {  
  2.   
  3.     SharedPreferences preferences = getApplicationContext().getSharedPreferences("test", Context.MODE_MULTI_PROCESS);  
  4.     SharedPreferences.Editor editor = preferences.edit();  
  5.     editor.putInt("pos", pos);  
  6.     editor.commit();  
  7.   
  8. }  
  9.   
  10. public int writeSharedprefs() {  
  11.     SharedPreferences preferences = getApplicationContext().getSharedPreferences("test", Context.MODE_MULTI_PROCESS);  
  12.     int pos = preferences.getInt("pos"0);  
  13.     return pos;  
  14. }  

检查下自己程序中是否涉及到跨进程保存和访问sharedPreferences。深刻的教训哦!!!

0 0
原创粉丝点击