0

I am trying to store the SIM Serial number as a value into the SharedPreferences but when i do a Toast to test for the value it is empty.

    TelephonyManager tMgr=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
    String ss = tMgr.getSimSerialNumber();

    // Writing data to SharedPreferences
    Editor editor = sp.edit();
    editor.putString("serial", ss);
    editor.commit();

    String value = settings.getString("serial", "");
    Toast.makeText(this, value, Toast.LENGTH_LONG).show();  
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
dythe
  • 840
  • 5
  • 21
  • 45

3 Answers3

1
  • Storing the serial:

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString("serial", ss);
    editor.commit();
    
  • Retrieving the serial:

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    String value = prefs.getString("serial", "000000000000");
    

where 000000000000 is the default value that will be returned to you if the sharedPreferences is not stored.

iTurki
  • 16,292
  • 20
  • 87
  • 132
1
   public void saveSerial(String serial){
      SharedPreferences prefs = getSharedPreferences("SIMSerial",Context.MODE_PRIVATE);
      SharedPreferences.Editor editor = prefs.edit();
      editor.putString("Serial", serial);
      editor.commit();
     }
    public String getSerial(){
      String getSIMSerial;
      SharedPreferences prefs = getSharedPreferences("SIMSerial",Context.MODE_PRIVATE);
      getSIMSerial = prefs.getString("Serial", "Default value");
        return getSIMSerial;
     }

The serial number will be stored in a xml that you can found at /data/data/YOUR_APP/shared_prefs/

LuisM
  • 35
  • 5
0

Short answer: Check on how you're instantiating your settings variable. You were using sp.edit(), so it should probably be sp.getString too.

josephus
  • 8,284
  • 1
  • 37
  • 57