0

I am using BroadcastReceiver in android app to get OTP message automatically.But BroadcastReceiver is not working and don't find the bug so please help me.I put Log.d("onReciver","on") in onReceive() to check but its not working.

IncomingMessage.java

public class IncomingMessage extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        Log.d("onReciver","on");
        final Bundle bundle = intent.getExtras();
        try {
            if (bundle != null)
            {
                final Object[] pdusObj = (Object[]) bundle.get("pdus");
                for (int i = 0; i < pdusObj .length; i++)
                {
                    SmsMessage currentMessage = SmsMessage.createFromPdu((byte[])                                                                                                    pdusObj[i]);
                    String phoneNumber = currentMessage.getDisplayOriginatingAddress();
                    String senderNum = phoneNumber ;
                    Log.d("sender NUm",senderNum);
                    String message = currentMessage .getDisplayMessageBody();
                    try
                    {
                        if (senderNum.equals("(727)594-3351"))
                        {
                            OtpVarificationActivity Sms = new OtpVarificationActivity();
                            Sms.recivedSms(message );
                        }
                    }
                    catch(Exception e){
                        Log.e("BR error",e.toString());
                    }

                }
            }

        } catch (Exception e)
        {
            Log.e("BR error1",e.toString());

        }

    }
}

Manifest.xml

<uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
...
...
...
<receiver android:name="com.appsprotocol.dcntv.broadcast.IncomingMessage">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
            </intent-filter>
        </receiver>

3 Answers3

0

Here is my working example

Register Receiver in AndoridMenifest.xml :

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

Receiver Class :

public class SmsListener extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

        if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
            Bundle bundle = intent.getExtras();           //---get the SMS message passed in---
            SmsMessage[] msgs = null;
            String msg_from;
            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++) {
                        if(Build.VERSION.SDK_INT <= 22) {
                            msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                        }
                        else {
                            msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i], String.format(Locale.US, null));
                        }
                        msg_from = msgs[i].getOriginatingAddress();
                        String msgBody = msgs[i].getMessageBody();
                        Toast.makeText(context, "From -" + msg_from + " : Body- " + msgBody, Toast.LENGTH_LONG).show();
                        if (msg_from.equalsIgnoreCase("5555")) {
                            MainActivity.insertCode(msgBody);
                        }
                    }
                } catch (Exception e) {
//                            Log.d("Exception caught",e.getMessage());
                }
            }
        }
    }
}
Divyang Panchal
  • 1,889
  • 1
  • 19
  • 27
  • okk but my query is my BroadcastReceiver is not calling when i run app.If its calling then i can see log message which i put in onReceive() –  Jun 21 '16 at 09:51
0

Try adding priority to your receiver in manifest. This worked for me.

<receiver android:name=".receiver.SMSReceiver">
            <intent-filter android:priority="999">
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>
Vivek Mishra
  • 5,669
  • 9
  • 46
  • 84
0

Add enabled and exported in manifest file. Below you can find the code sample for manifest.

<receiver
            android:name=".SmsReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter android:priority="999">
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>
Jeevanandhan
  • 1,073
  • 10
  • 18
  • @RahulDev:: are you using marshmellow? and make sure you have implemented the [BRAODCAST_SMS] (https://developer.android.com/reference/android/Manifest.permission.html#BROADCAST_SMS) permission. – Jeevanandhan Jun 21 '16 at 10:38
  • yes i am using it and try to use BROADCAST_SMS permission but it through me error "permission is only granted to the system apps".whats the mean of this error? –  Jun 22 '16 at 08:13
  • I got it but its not problem of BROADCAST_SMS permission.Its because API 23 did't accept permission automatically.I set it going to mobile app permissions and its working. thanks for guiding me –  Jun 22 '16 at 09:10