Android Sms Broadcast Listener not working

2019-07-11 04:36发布

问题:

Even the Log.d is not being called. Can't find the error. I have looked at Android - SMS Broadcast receiver, Android – Listen For Incoming SMS Messages. I have been looking for the solution for last 24 hours or so. Please see and tell where I am wrong.

No exceptions are thrown. SmsListener is in my package folder, where all other activities are placed.

Update added android:enabled="true" to receiver. Then also not worked.

I have in my manifest:

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

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

SmsListener.Java Code:

public class SmsListener extends BroadcastReceiver {

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

    Log.d(MainActivity.TAG,
            "in Receiver. intent.getAction():" + intent.getAction());

    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++) {
                    msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                    msg_from = msgs[i].getOriginatingAddress();
                    String msgBody = msgs[i].getMessageBody();
                    NotifyMe(context, msg_from, msgBody);
                }
            } catch (Exception e) {
                // Log.d("Exception caught",e.getMessage());
            }
        }
    }
}
private void NotifyMe(Context context, String msg_from, String msgBody) {
     // do something
}
}

回答1:

The problem is now solved. Modified receiver in manifest to

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

It works for me. Hope someone gets benefited by this. Thanks everyone for the help.



回答2:

Did you tried to register receiver by the code? Below is for example how I did it in one of my projects, - it is working in my case.

SMS Receiver class

public class SMSReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        Object[] pduArray = (Object[]) intent.getExtras().get("pdus");
        SmsMessage[] messages = new SmsMessage[pduArray.length];
        for (int i = 0; i < pduArray.length; i++) messages[i] = SmsMessage.createFromPdu((byte[]) pduArray[i]);

        String SideNumber = messages[0].getDisplayOriginatingAddress();
        long Timestamp = messages[0].getTimestampMillis();

        StringBuilder bt = new StringBuilder();
        for (SmsMessage message : messages) bt.append(message.getMessageBody());

        String Smsbody = bt.toString();
    }
}

Service class - it registers broadcast receiver This is just for example, you can register broadcast receiver in your Activity.

public class AService extends Service
{
  private SMSReceiver smsReceiver;
  final IntentFilter smsFilter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");

  @Override
    public void onCreate()
    {
     smsFilter.setPriority(1000);
     this.smsReceiver = new SMSReceiver();
     this.registerReceiver(this.smsReceiver, smsFilter);
    }
}

Part of AndroidManifest.xml

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

<service android:name=".AService" android:exported="false"/>


回答3:

Do you have this permission in your manifest android.permission.RECEIVE_SMS?

This is basic, but just make sure