2

I was trying to read incoming text messages and perform some actions based on the text in the SMS but the onReceive() method is not triggered.

Manifest File

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.dost.anshdeep.dosti">
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.READ_SMS" />
    <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"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Ringing"
            android:label="@string/title_activity_ringing"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="com.dost.anshdeep.dosti.Ringing" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Alarms"
            android:label="@string/title_activity_alarms"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="com.dost.anshdeep.dosti.Alarms" />

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

    </application>

</manifest>

SmsListener.java

 public class SmsListener extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context,"Hello",Toast.LENGTH_LONG).show();
        //Log.d("Message Received : ",msgBody);
        if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
            Bundle bundle = intent.getExtras();           //---get the SMS message passed in---
            SmsMessage[] msgs = null;
            if (bundle != null){
                //---retrieve the SMS message received---
                try{
                    Object[] pdus = (Object[]) bundle.get("pdus");
                    msgs = new SmsMessage[pdus.length];
                    for(int i=0; i<msgs.length; i++){
                        msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                        String msg_from = msgs[i].getOriginatingAddress();
                        String msgBody = msgs[i].getMessageBody();
                    }
                }catch(Exception e){
                            Log.d("Exception caught", e.getMessage());
                }
            }
        }
        //Toast.makeText(context,msg_from,Toast.LENGTH_LONG);
    }
}

Can I know what is the issue with this code as I can't figure out why the onReceive() is not called.

Deepanshu
  • 29
  • 3
  • I really appreciate your help Arshad but its not working for me. And I wished to know the reason why my code is not working. Still the onReceive() is not invoked. Any help will be greatly appreciated. – Deepanshu Mar 13 '16 at 08:39

2 Answers2

2

Try declaring your BroadcastReceiver like this in Manifest

<receiver
        android:name=".SmsReceiver"
        android:exported="true"
        android:permission="android.permission.BROADCAST_SMS">

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

    </receiver>

and Your BroadCast Receiver

public class SmsReceiver extends BroadcastReceiver {

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

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

    Log.e(TAG, "Checking Sms");
    Bundle bundle = intent.getExtras();
    try{
        if(bundle != null){
            Object[] pdusObj = (Object[]) bundle.get("pdus");
            for (Object aPdusObj : pdusObj) {
                SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) aPdusObj);
                String senderAddress = currentMessage.getDisplayOriginatingAddress();
                String message = currentMessage.getDisplayMessageBody();

                Log.e(TAG, "Received SMS: " + message + ", Sender: " + senderAddress);

                if (!senderAddress.toLowerCase().contains(GlobalFunctions.SMS_ORIGIN.toLowerCase())) {
                    return;
                }

                // verification code from sms
                String verificationCode = getVerificationCode(message);

                Log.e(TAG, "OTP received: " + verificationCode);

                if(AppController.isActivityVisible()) {
                    Intent smsService = new Intent(context, SmsService.class);
                    smsService.putExtra("otp", verificationCode);
                    context.startService(smsService);
                }
            }
        }
    } catch (Exception e){
        Log.e(TAG, "Exception");
        e.printStackTrace();
    }

}

}

Note: Do not copy the whole code, it contains my declared variables. Just follow the code and write your own as per your neeed

Arshad
  • 1,262
  • 16
  • 25
  • Please note that if another application declares a broadcast receiver listening for the same intent with higher priority and does not pass on the intent then this all lower priority broadcast receivers will not receive the intent. Handcent SMS is known to do this. – aashreys Mar 13 '16 at 00:23
1

On API 23 and above, you should also get permission to receive SMS at runtime. look here

Mr.Q
  • 4,316
  • 3
  • 43
  • 40