I have written code for Broadcast Receiver that toasts when an SMS is received, and it's not working can anyone rectify the mistake? : This is the MainActivity.java


    package tagit.aj.com.broadcastreceiverforsms;
    
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    }


This the MyReceiver class which includes the broadcast receiver's onReceive method. One of the methods used here is "deprecated" but I assume it does not creates any problem in testing. 


    package tagit.aj.com.broadcastreceiverforsms;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.telephony.SmsMessage;
    import android.util.Log;
    import android.widget.Toast;
    
        public class MyReceiver extends BroadcastReceiver {
        public void onReceive(Context context, Intent intent) {
    
            Toast.makeText(context,"IncomingSms",Toast.LENGTH_SHORT).show();
    
            // Retrieves a map of extended data from the intent.
            final Bundle bundle = intent.getExtras();
    
            try {
                if (bundle != null) {
                    Object[] pdus = (Object[]) bundle.get("pdus");
    
                    for (int i = 0; i < pdus.length; i++) {
    
                        SmsMessage messages = SmsMessage.createFromPdu((byte[]) pdus[i]);
    
                        String phoneNumber = messages.getDisplayOriginatingAddress();
                        String phone = phoneNumber;
                        String stringMessage = messages.getDisplayMessageBody();
    
                        int duration = Toast.LENGTH_LONG;
                        Log.i("Broadcasting", "Number" + phone + "Message" + stringMessage);
                        Toast toast = Toast.makeText(context, "senderNum: " + phone + ", message: " + stringMessage, duration);
                        toast.show();
    
                    } // end for loop
                } // bundle is null
            } catch (Exception e) {
                Log.e("SmsReceiver", "Exception smsReceiver" + e);
            }
        }
    }


The XML layout is

        <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/activity_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingBottom="@dimen/activity_vertical_margin"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin"
            tools:context="tagit.aj.com.broadcastreceiverforsms.MainActivity">
        
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hello World!" />
        </RelativeLayout>
    
    
And the AndroidManifest file is:-
    
    
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="tagit.aj.com.broadcastreceiverforsms">
        <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
        <uses-permission android:name="android.permission.READ_SMS"></uses-permission>
        <uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            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=".MyReceiver"
                android:enabled="true"
                android:exported="true">
                <intent-filter>
                    <action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
                </intent-filter>
            </receiver>
        </application>
    
    </manifest>


Please provide me with why this problem is occurring. and solution (if any). And i'm running this app in Android KitKat