title: Using shared preference to save the string and send the saved string as sms to another number
body: <p>I have to send my own mobile number as sms to another number say xxx i used getLine1number() function it is returning null since it does not have the number stored in the sim.
  I am trying to Enter my number save it once using shared preference and send that number as sms to another number.. how it is possible?My code is</p>
  
  <pre><code>package com.example.mobile;
  
  import android.os.Bundle;
  import android.telephony.SmsManager;
  import android.content.SharedPreferences;
  import android.content.SharedPreferences.Editor;
  import android.preference.PreferenceManager;
  import android.view.View;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.Toast;
  import android.app.Activity;
  import android.content.Context;
  import android.telephony.TelephonyManager;
  
  public class MainActivity extends Activity {
  
  EditText editText;
  Button saveButton;
  
  String mStoredNumber;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      editText = (EditText) findViewById(R.id.editText1);
      saveButton = (Button) findViewById(R.id.button1);
  
      saveButton.setOnClickListener(new View.OnClickListener(){
          public void onClick(View v) {
                      savePreferences("storedNumber", editText.getText().toString());
                      Toast.makeText(getApplicationContext(), "Saved", Toast.LENGTH_LONG).show();
  
              // Load number and send to phoneNo
              loadSavedPreferences();
              // Now the stored number is in mStoredNumber
              String phoneNo = "123";
  
              TelephonyManager tManager=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
              String Imsi=tManager.getSubscriberId();
              String Imei=tManager.getDeviceId();
  
              try{
              SmsManager smsManager = SmsManager.getDefault();
              smsManager.sendTextMessage(phoneNo, null, "IMSI,IMEI and phone number - "+Imsi+","+Imei+","+mStoredNumber+"", null, null);
              Toast.makeText(getApplicationContext(), "SMS Sent!",
                          Toast.LENGTH_LONG).show();
              }
              catch (Exception e) {
                  Toast.makeText(getApplicationContext(),
                      "SMS faild, please try again later!",
                      Toast.LENGTH_LONG).show();
                  e.printStackTrace();
                }
  
      loadSavedPreferences();
  }
       });
  }
  
  private void loadSavedPreferences() {
      SharedPreferences sharedPreferences = PreferenceManager
              .getDefaultSharedPreferences(this);
  
      String number = sharedPreferences.getString("storedNumber", "YourNumber");
      mStoredNumber = number;
  
      editText.setText(number);
  }
  
  private void savePreferences(String key, String value) {
      SharedPreferences sharedPreferences = PreferenceManager
              .getDefaultSharedPreferences(this);
      Editor editor = sharedPreferences.edit();
      editor.putString(key, value);
      editor.commit();
  
  }
  }
  </code></pre>
  
  <p>My xml code is </p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:id="@+id/linearLayout1" 
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent" 
  android:orientation="vertical"&gt;
  
  &lt;EditText
      android:id="@+id/editText1"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="@string/enter_your_number_" 
      android:inputType="number"&gt;
  
      &lt;requestFocus /&gt;
  &lt;/EditText&gt;
  
  &lt;Button android:id="@+id/button1" 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:text="@string/save"/&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>The number is getting saved and when i again open the application it is not showing the number on the top its again asking for enter the number. And second time when i open the app i should not get the save button.
  Please suggest a solution.</p>