title: how to get the incoming SMS in android?
body: <p>i want to print the toast message when the sms send by another device. I wrote the code. but it is not working. please any one help me out.
  android version: 4.0.4
  MainActivty:</p>
  
  <pre><code>public class MainActivity extends ActionBarActivity {
      Button btnSendSMS;
      EditText txtPhoneNo;
      EditText txtMessage;
      BroadcastReceiver receiver = new SmsReceiver();
  
      @Override
      protected void onStop() {
          super.onStop();
          unregisterReceiver(receiver);
      }
  
      @Override
      protected void onStart() {
          super.onStart();
          IntentFilter intentFilter = new IntentFilter("some_string-to_call_receiver");
          registerReceiver(receiver, intentFilter);
      }
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
          txtPhoneNo = (EditText) findViewById(R.id.txtPhoneNo);
          txtMessage = (EditText) findViewById(R.id.txtMessage);
  
          sendBroadcast(new Intent("some_string-to_call_receiver"));
  
      }
  }
  </code></pre>
  
  <p>manifest:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.sms" &gt;
      &lt;uses-permission android:name="android.permission.SEND_SMS"&gt;
      &lt;/uses-permission&gt;
      &lt;uses-permission android:name="android.permission.RECEIVE_SMS"&gt;
      &lt;/uses-permission&gt;
  </code></pre>
  
  <p></p>
  
  <pre><code>    &lt;application
          android:allowBackup="true"
          android:icon="@mipmap/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" &gt;
          &lt;activity
              android:name=".MainActivity"
              android:label="@string/app_name" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
      &lt;/application&gt;
      &lt;receiver android:name=".SmsReceiver"&gt;
          &lt;intent-filter&gt;
              &lt;action android:name=
                  "android.provider.Telephony.SMS_RECEIVED" /&gt;
          &lt;/intent-filter&gt;
      &lt;/receiver&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p>smsreceiver:</p>
  
  <pre><code>public class SmsReceiver   extends BroadcastReceiver {
      public static String SERVER_SUCCESS_CODE = "";
  
      // Get the object of SmsManager
      final SmsManager sms = SmsManager.getDefault();
  
      public void onReceive(Context context, Intent intent) {
  
          // Retrieves a map of extended data from the intent.
          final Bundle bundle = intent.getExtras();
  
          try {
  
              if (bundle != null) {
  
                  final Object[] pdusObj = (Object[]) bundle.get("pdus");
  
                  for (int i = 0; i &lt; pdusObj.length; i++) {
  
                      SmsMessage currentMessage = SmsMessage
                              .createFromPdu((byte[]) pdusObj[i]);
                      String phoneNumber = currentMessage
                              .getDisplayOriginatingAddress();
  
                      String senderNum = phoneNumber;
                      String message = currentMessage.getDisplayMessageBody();
                      String[] split_one = message.split(":");
                      String  ss = split_one[1].substring(0, 4);
                      if(ss.length()==4)
                      {
                          SERVER_SUCCESS_CODE=ss;
                      }
  
                      /*Log.i("SmsReceiver", "senderNum: " + senderNum
                              + "; message: " + message);
  */
                      // Show Alert
                  int duration = Toast.LENGTH_LONG;
                  Toast toast = Toast.makeText(context, "senderNum: "
                          + senderNum + ", message: " + message, duration);
                  toast.show();
  
                  }
              }
  
          }
          catch (Exception e) {
              Log.e("SmsReceiver", "Exception smsReceiver" + e);
  
          }
      }
  }
  </code></pre>
  
  <p>how i get the incoming sms and toast printing!</p>