SharedPreferences保存取值不可取的坑

    xiaoxiao2021-04-16  38

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

    public void writeSharedprefs(int pos) {          SharedPreferences preferences = getApplicationContext().getSharedPreferences("test", Context.MODE_MULTI_PROCESS);       SharedPreferences.Editor editor = preferences.edit();       editor.putInt("pos", pos);       editor.commit();      }      public int writeSharedprefs() {       SharedPreferences preferences = getApplicationContext().getSharedPreferences("test", Context.MODE_MULTI_PROCESS);       int pos = preferences.getInt("pos"0);       return pos;   }  

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

    转载请注明原文地址: https://ju.6miu.com/read-672367.html

    最新回复(0)