0

I'm building an application that should trigger a function when an SMS is received. I've used Broadcast Receivers and NotificationListeners before, but for this specific purpose, I need to register this Broadcast receiver. At this time, I'm unable to register it, and I can't quite tell why.

I've tried multiple answers to similar issues on stack overflow but for some reason, the Log.d that I'm using to troubleshoot right now will not print when I receive an SMS. I think I'm fundamentally misunderstanding the problem, although I'm not sure how.

Here is my SmsListener.java class:

public class SmsListener extends BroadcastReceiver {
    private SharedPreferences preferences;

    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
            Log.d("Test","test");
        }
    }
}

Here is my Manifest:

<receiver android:name=".SmsListener">
    <intent-filter>
        <action android:name="android.provider.Telephony.SMS_RECEIVED" />
    </intent-filter>
</receiver>

Here is the onCreate() function of my activity that should be calling it:

BroadcastReceiver br = new SmsListener();
IntentFilter filter = new IntentFilter(Telephony.Sms.Intents.SMS_RECEIVED_ACTION);
registerReceiver(br,filter);

Thank you any help. I'm at a total loss.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
Alec Benson
  • 55
  • 1
  • 4
  • Have you requested for permissions ? – Shivam Verma Apr 24 '19 at 22:50
  • Yes, I ask for the RECEIVE_SMS permission – Alec Benson Apr 24 '19 at 22:59
  • Did you request it at runtime, in addition to the manifest entry? Are you testing on an emulator, or an actual device? Btw, if you're statically registering that in the manifest, you don't necessarily need to dynamically register an instance in your code. – Mike M. Apr 24 '19 at 23:07
  • I use ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.RECEIVE_SMS},1); to request the permission, and I am testing on a one plus physical device – Alec Benson Apr 24 '19 at 23:08
  • I'm not personally familiar with OnePlus, but I wouldn't be surprised if it has extra restrictions on third-party apps receiving SMS, or running in the background, etc. Look in the device Settings for any additional security-related type things that might be preventing that broadcast to your app. It might also be in a separate, standalone app. – Mike M. Apr 24 '19 at 23:14
  • Relevant links: https://stackoverflow.com/a/52016035. https://stackoverflow.com/a/46225122, https://stackoverflow.com/a/53538918. – Mike M. Apr 24 '19 at 23:19

2 Answers2

1

In your fragment

val smsReciever = SmsReciever()
val smsIntentFilter = IntentFilter(Telephony.Sms.Intents.SMS_RECEIVED_ACTION)
context?.registerReceiver(smsReciever, smsIntentFilter)      

In your manifest

<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>

In your receiver

class SmsReciever : BroadcastReceiver() {

   override fun onReceive(context: Context, intent: Intent) {

        val message = Telephony.Sms.Intents.getMessagesFromIntent(intent)
        val content =  message[0].displayMessageBody

        Toast.makeText(context, content,
           Toast.LENGTH_SHORT).show()
    }
}
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
hamid
  • 47
  • 5
0

This is Your Receiver Class-

private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED"; private static final String TAG = "SMSBroadcastReceiver";

@Override
public void onReceive(Context context, Intent intent) {

    if (intent.getAction() != null) {
        if (intent.getAction().equals(SMS_RECEIVED)) {
            Bundle bundle = intent.getExtras();
            if (bundle != null) {
                Object[] pdus = (Object[]) bundle.get("pdus");
                final SmsMessage[] messages = new SmsMessage[pdus.length];
                for (int i = 0; i < pdus.length; i++) {
                    messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                    Log.e("Message Content : ", " == " + messages[i].getMessageBody());
                    Log.e("Message Content Body : ", " == " + messages[i].getDisplayMessageBody());
                    Log.e("Message recieved From", " == " + messages[0].getOriginatingAddress());
                }
            /*if (messages.length > -1) {
                Log.e("Message recieved: "," == "+ messages[0].getMessageBody());
                Log.e("Message recieved From"," == "+ messages[0].getOriginatingAddress());
            }*/
            }
        }
    }
}
D. Rathore
  • 27
  • 6