9

Hi I'm trying to catch the sms content and use in my app, so I made a BroadcastReceiver with Permission and Manifests But when the device receive sms, my code doesn't run which means that the BroadcastReceiver doesn't fire. I also checked many Articles inside and outside here, there're some:

Android Sms Receiver Result to Main Activity SMS receiver didn't work

Android SMS Receiver not working

Broadcast Receiver not working for SMS

Here is a part of my Manifest:

<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<application.
...
...
<receiver android:name="com.example.android.receiver.SmsReceiver"
            android:permission="android.permission.BROADCAST_SMS">
            <intent-filter android:priority="2147483647">
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>
</application>

And This is my Receiver

public class SmsReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "SMS Received!", Toast.LENGTH_LONG).show();
    }
}

I also tried to register the receiver dynamically inside activity onCreate() but nothing changed

IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
intentFilter.setPriority(2147483647);
registerReceiver(new SmsReceiver(), intentFilter);

Does anyone know where is the problem? It should just Toast that a message is recieved so I can continue work but the receiver doesn't seem to even fire

Community
  • 1
  • 1
AMINEDGE
  • 439
  • 3
  • 12
  • Any error during compilation? I mean those dots in your `manifest`. Are they actually there? Do you have any activity in your app? – ray an May 13 '17 at 20:38
  • Also, On Android 3.1 and higher, the user must launch one of your activities before any manifest-registered `BroadcastReceiver` will work. – ray an May 13 '17 at 20:41
  • No error I run the app on my device, There's no dots only the common properties such as icon or name etc. yea of course I have several activities, and yes I run the app and open that activity which registers the receiver. – AMINEDGE May 13 '17 at 20:41
  • You need to open your app first after installing it for broadcast receiver to work. – ray an May 13 '17 at 20:43
  • Yea as I said I open the app first – AMINEDGE May 13 '17 at 20:47
  • Did you use runtime permission to receive SMS? – Green Y. Feb 15 '20 at 09:19

4 Answers4

4

You should read Automatic SMS Verification.

public abstract Task startSmsRetriever ()

Starts SmsRetriever, which waits for a matching SMS message until timeout (5 minutes). The matching SMS message will be sent via a Broadcast Intent with action SmsRetriever.SMS_RETRIEVED_ACTION.

When you are ready to verify the user's phone number, get an instance of the SmsRetrieverClient object, call startSmsRetriever, and attach success and failure listeners to the SMS retrieval task:

SmsRetrieverClient mClient = SmsRetriever.getClient(this);
Task<Void> mTask = mClient.startSmsRetriever();
mTask.addOnSuccessListener(new OnSuccessListener<Void>() {
  @Override public void onSuccess(Void aVoid) {
    Toast.makeText(YourActivity.this, "SMS Retriever starts", Toast.LENGTH_LONG).show();
  }
});
mTask.addOnFailureListener(new OnFailureListener() {
  @Override public void onFailure(@NonNull Exception e) {
    Toast.makeText(YourActivity.this, "Error", Toast.LENGTH_LONG).show();
  }
});

When a verification message is received on the user's device, Play services explicitly broadcasts to your app a SmsRetriever.SMS_RETRIEVED_ACTION Intent, which contains the text of the message. Use a BroadcastReceiver to receive this verification message.

public void onReceive(Context context, Intent intent) {
    if (SmsRetriever.SMS_RETRIEVED_ACTION.equals(intent.getAction())) {
      Bundle extras = intent.getExtras();
      Status status = (Status) extras.get(SmsRetriever.EXTRA_STATUS);

      switch(status.getStatusCode()) {
        case CommonStatusCodes.SUCCESS:
          // Get SMS message contents
          String message = (String) extras.get(SmsRetriever.EXTRA_SMS_MESSAGE);
          // Extract one-time code from the message and complete verification
          // by sending the code back to your server.
          break;
        case CommonStatusCodes.TIMEOUT:
          // Waiting for SMS timed out (5 minutes)
          // Handle the error ...
          break;
      }
    }
  }

Register Your BroadcastReceiver with the intent filter com.google.android.gms.auth.api.phone.SMS_RETRIEVED in your app's AndroidManifest.xml file.

<receiver android:name=".YourBroadcastReceiver" android:exported="true">
    <intent-filter>
        <action android:name="com.google.android.gms.auth.api.phone.SMS_RETRIEVED"/>
    </intent-filter>
</receiver>

Finally, Register your BroadcastReceiver in your onCreate() section.

IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(SmsRetriever.SMS_RETRIEVED_ACTION);
getApplicationContext().registerReceiver(broadcastReceiverOBJ, intentFilter);

For Demo purpose you should read Automatic SMS Verification Android

IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
0

I found it myself. Here is code that works! It must contain SMS_DELIVER_ACTION which this does. (many on github do not!)

Go into Settings->AppsNotifications->DefaultApps->MessagingApp and "SMS App" will showup for you to pick.

https://android-developers.googleblog.com/2013/10/getting-your-sms-apps-ready-for-kitkat.html https://www.androidauthority.com/how-to-create-an-sms-app-part-2-724264/ https://github.com/treehousefrog/SMS-Project-Part-2

jim
  • 228
  • 4
  • 10
0

You should request runtime permission to receive SMS(Android 6.0 and higher).

https://developer.android.com/guide/topics/permissions/overview

Green Y.
  • 445
  • 1
  • 8
  • 19
0

First make another app your default SMS app.

Then: Google Hangout --> Settings(Disable merged conversations) --> SMS (Disable SMS)

Or,

This example for mainfest:

         <receiver android:name=".SmsBroadcastReceiver"
            android:exported="true"
            android:permission="android.permission.BROADCAST_SMS">
            <intent-filter android:priority="999" >

                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
                <action android:name="android.provider.Telephony.SMS_DELIVER" />
                <action android:name="android.provider.Telephony.SMS_DELIVER_ACTION" />
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
        </receiver>
Waqar UlHaq
  • 6,144
  • 2
  • 34
  • 42
Jamil Hasnine Tamim
  • 4,389
  • 27
  • 43