Search code examples
androidbroadcastreceiver

Unable to receive broadcast message android


I have registered "SMS received event" for the broadcast receiver but when i launch the app and send message to the emulator i do not see onReceive method of broadcast receiver getting triggered.

AndroidManifest.xml

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

    <uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.permission.RECEIVE_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">


        <receiver android:name=".MyReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter android:priority="500">
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />

            </intent-filter>
        </receiver>

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

MyReceiver.java

public class MyReceiver extends BroadcastReceiver {


    private static final String TAG =
            MyReceiver.class.getSimpleName();


    @Override
    public void onReceive(Context context, Intent intent) {
        // Get the SMS message.
        Log.v(TAG,"Message Received");
   }


Solution

  • I think you need to check permission in code for SDK more or equal to M(Marshmallow) -

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
      {
          if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission. RECEIVE_SMS) == PackageManager.PERMISSION_GRANTED) {
          //  you can receive sms   
          } 
          else
          {
             ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission. RECEIVE_SMS}, 411);
          }
      }
      else
      {
        //  you can receive sms          
      }
    }
    

    And on result of user action-

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 411) {
            if (grantResults.length == 0 || grantResults == null) {
                 // show dialog that you need access to go ahead
            } else if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Your code here permission granted
            } else if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
                 // show dialog that you need access to go ahead
            }
        }
    }