Search code examples
javaandroidbroadcastreceiversmsandroid-broadcastreceiver

Monitoring an incoming SMS with Toast


I want to monitor an incoming sms in a phone by making a Toast message pop up when a sms is received. However, with the following code, I don't receive the Toast message even when a sms is received. Why is that so? I'm using Android Studio 3.0.1 and there are no errors when I run the code.

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="mapp.com.sg.broadcastreceiver">

    <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>
    </application>

</manifest>

MainActivity.java

public class MainActivity extends Activity {
    private static final int NOTIFY_ME_ID = 1337;

    private BroadcastReceiver the_receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            int duration = Toast.LENGTH_LONG;
            Toast toast = Toast.makeText(context, "SMS Message Received!", duration);
            toast.show();
        }
    };

    private IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onResume() {
        // Register receiver if activity is in front
        this.registerReceiver(the_receiver, filter);
        super.onResume();
    }

    @Override
    protected void onPause() {
        // Unregister receiver if activity is not in front
        this.unregisterReceiver(the_receiver);
        super.onPause();
    }
}

Solution

  • Do as following by adding the receiver in the manifest file

       <receiver
            android:name=".receivers.SMSReceiver"
            android:permission="android.permission.BROADCAST_SMS">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
       </receiver>
    

    and then create SMSReceiver class

    public class SMSReceiver extends BroadcastReceiver {
        public void onReceive(Context context, Intent intent) {
            Bundle myBundle = intent.getExtras();
            SmsMessage[] messages;
    
            if (myBundle != null) {
    
                Toast toast = Toast.makeText(context, "SMS Message Received!",duration);
            }
        }
    }