I have a button in an activity. By clicking on it an SMS with a predefined body and phone address is sent, then a reply SMS including a code is automatically sent to my mobile phone . I need to receive the code and set it to my textview. How can I solve it? I have two activities : MainActivity and MessageReceiver.

```
    //MessageReceiver.java
    public class MessageReceiver extends BroadcastReceiver {
        public void onReceive(Context context, Intent intent) {

        Bundle bundle = intent.getExtras();
        SmsMessage[] messages;
        String str = "";

        if (bundle != null) {
            Object[] pdus = (Object[]) bundle.get("pdus");
            messages = new SmsMessage[pdus != null ? pdus.length : 0];
            for (int i = 0; i < messages.length; i++) {
                messages[i] = SmsMessage.createFromPdu((byte[]) (pdus != null ? pdus[i] : null));
                str += messages[i].getOriginatingAddress();
                str += ":";
                str += messages[i].getMessageBody();
                str += "\n";
            }
            Intent broadcastIntent = new Intent();
            broadcastIntent.setAction("SMS_RECIEVED_ACTION");
            broadcastIntent.putExtra("message", str);
            context.sendBroadcast(broadcastIntent);}}}

```

---

```
     //MainActivity.java
     private BroadcastReceiver intentReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            TextView chargeText = (TextView) findViewById(R.id.chargeText);
            Log.d("myTag", "is chargeText null? : " + (chargeText==null));
            Log.d("myTag", "The text is: " + (intent.getExtras().getString("message")));
            chargeText.setText(intent.getExtras().getString("message"));
        }
    };

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        intentFilter = new IntentFilter();
        intentFilter.addAction("SMS_RECEIVED_ACTION");
 button3 = (Button) findViewById(R.id.button);

 button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String mymsg = "CMDACC_1234";
                String thenumber = "09380638202";
                SendChargeMessage(thenumber, mymsg);
            }
        });}

    public void SendChargeMessage(String thenumber, String mymsg) {
        String SENT = "Message sent";
        String DELIVERED = "Message delivered";
        SmsManager smsManager = SmsManager.getDefault();
        Context curContext = this.getApplicationContext();
        PendingIntent sentPending = PendingIntent.getBroadcast(curContext,
                0, new Intent("SENT"), 0);
        curContext.registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode()) {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "Sent.",
                                Toast.LENGTH_LONG).show();
                        break;
                        case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Toast.makeText(getBaseContext(), "Not Sent: Generic failure.",
                                Toast.LENGTH_LONG).show();
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        Toast.makeText(getBaseContext(), "Not Sent: No service (possibly, no SIM-card).",
                        Toast.LENGTH_LONG).show();
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        Toast.makeText(getBaseContext(), "Not Sent: Null PDU.",
                        Toast.LENGTH_LONG).show();
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        Toast.makeText(getBaseContext(), "Not Sent: Radio off (possibly, Airplane mode enabled in Settings).",
                                Toast.LENGTH_LONG).show();
                        break;
                }
            }
        }, new IntentFilter("SENT"));

        PendingIntent deliveredPending = PendingIntent.getBroadcast(curContext,
                0, new Intent("DELIVERED"), 0);

        curContext.registerReceiver(
                new BroadcastReceiver() {
                    @Override
                    public void onReceive(Context arg0, Intent arg1) {
                        switch (getResultCode()) {
                            case Activity.RESULT_OK:
                                Toast.makeText(getBaseContext(), "Delivered.",
                                        Toast.LENGTH_LONG).show();
                                break;
                            case Activity.RESULT_CANCELED:
                                Toast.makeText(getBaseContext(), "Not Delivered: Canceled.",
                                        Toast.LENGTH_LONG).show();
                                break;
                        }
                    }
                }, new IntentFilter("DELIVERED"));

        PackageManager pm = this.getPackageManager();

        if (!pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY) &&
                !pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY_CDMA)) {
            Toast.makeText(this, "Sorry, your device probably can't send SMS...", Toast.LENGTH_SHORT).show();
        } else {
            smsManager.sendTextMessage("09380638202", null, "CMDACC_1234", sentPendin

g, deliveredPending);
                //chargeText.setText(SMSBody1);
            }
        }

///Edited

 @Override
protected void onResume(){
    super.onResume();
    IntentFilter intentFilter;
    intentFilter = new IntentFilter();
    intentFilter.addAction("SMS_RECEIVED_ACTION");

    BroadcastReceiver intentReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            TextView chargeText = (TextView) findViewById(R.id.chargeText);
            Log.d("myTag", "is chargeText null? : " + (chargeText==null));

            Log.d("myTag", "The text is: " + (intent.getExtras().getString("message")));

            String text = intent.getExtras().getString("message").toString(); // let's try this with toString() so we are very explicit about it

            Log.d("myTag", "The converted text is: " + text);

            chargeText.setText(text);
        }
    };

    registerReceiver(intentReceiver , intentFilter);

} }

```

manifest.xml 

```
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.dearzeinab.emaapplication">

    <uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_SMS" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name="com.example.dearzeinab.emaapplication.MessageReceiver" android:enabled="true">
            <intent-filter android:priority="2147483647">
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>

    </application>

</manifest>

```