-1

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");
   }

2 Answers2

1

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
        }
    }
}
Dipankar Baghel
  • 1,932
  • 2
  • 12
  • 24
0

The android.permission.RECEIVE_SMS permission is a dangerous permission. You should use ActivityCompat.requestPermissions to request it.

<permission android:name="android.permission.RECEIVE_SMS"
    android:permissionGroup="android.permission-group.UNDEFINED"
    android:label="@string/permlab_receiveSms"
    android:description="@string/permdesc_receiveSms"
    android:permissionFlags="hardRestricted"
    android:protectionLevel="dangerous" />
Zhanwei
  • 1
  • 1