Android - SMS Broadcast receiver

前端 未结 10 1710
遥遥无期
遥遥无期 2020-11-22 13:04

I have been trying to get this program to work but so far having no luck. I cannot find where I am doing wrong. I\'m not sure if there\'s something wrong with the code, or d

相关标签:
10条回答
  • 2020-11-22 13:42

    I tried your code and found it wasn't working.

    I had to change

    if (intent.getAction() == SMS_RECEIVED) {
    

    to

    if (intent.getAction().equals(SMS_RECEIVED)) {
    

    Now it's working. It's just an issue with java checking equality.

    0 讨论(0)
  • 2020-11-22 13:44

    The Updated code is :

     private class SMSReceiver extends BroadcastReceiver {
                    private Bundle bundle;
                    private SmsMessage currentSMS;
                    private String message;
    
                    @Override
                    public void onReceive(Context context, Intent intent) {
    
                        if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
                            bundle = intent.getExtras();
                            if (bundle != null) {
                                Object[] pdu_Objects = (Object[]) bundle.get("pdus");
                                if (pdu_Objects != null) {
    
                                    for (Object aObject : pdu_Objects) {
    
                                        currentSMS = getIncomingMessage(aObject, bundle);
    
                                        String senderNo = currentSMS.getDisplayOriginatingAddress();
    
                                        message = currentSMS.getDisplayMessageBody();
                                        Toast.makeText(OtpActivity.this, "senderNum: " + senderNo + " :\n message: " + message, Toast.LENGTH_LONG).show();
                                    }
                                    this.abortBroadcast();
                                    // End of loop
                                }
                            }
                        } // bundle null
                    }
                }
    
            private SmsMessage getIncomingMessage(Object aObject, Bundle bundle) {
                SmsMessage currentSMS;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    String format = bundle.getString("format");
                    currentSMS = SmsMessage.createFromPdu((byte[]) aObject, format);
                } else {
                    currentSMS = SmsMessage.createFromPdu((byte[]) aObject);
                }
        return currentSMS;
    }
    

    older code was :

    Object [] pdus = (Object[]) myBundle.get("pdus");
    
        messages = new SmsMessage[pdus.length];
    
        for (int i = 0; i < messages.length; i++)
        {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                String format = myBundle.getString("format");
                messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i], format);
            }
            else {
                messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
            }
            strMessage += "SMS From: " + messages[i].getOriginatingAddress();
            strMessage += " : ";
            strMessage += messages[i].getMessageBody();
            strMessage += "\n";
        } 
    

    The simple SYntax of code is :

    private class SMSReceiver extends BroadcastReceiver {
    
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(Telephony.Sms.Intents.SMS_RECEIVED_ACTION)) {
                SmsMessage[] smsMessages = Telephony.Sms.Intents.getMessagesFromIntent(intent);
                for (SmsMessage message : smsMessages) {
                    // Do whatever you want to do with SMS.
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-22 13:48

    android.provider.telephony.SMS_RECEIVED is not correct because Telephony is a class and it should be capital as in android.provider.Telephony.SMS_RECEIVED

    0 讨论(0)
  • 2020-11-22 13:49

    For android 19+ you can get it in Telephony.Sms.Intents.SMS_RECEIVED_ACTION). There are more in the Intents class that 're worth looking at

    0 讨论(0)
  • 2020-11-22 13:50

    I've encountered such issue recently. Though code was correct, I didn't turn on permissions in app settings. So, all permissions hasn't been set by default on emulators, so you should do it yourself.

    0 讨论(0)
  • 2020-11-22 13:51

    android.provider.Telephony.SMS_RECEIVED has a capital T, and yours in the manifest does not.

    Please bear in mind that this Intent action is not documented.

    0 讨论(0)
提交回复
热议问题