Search code examples
androidsmsmanager

SMS is not received on dual sim


I write a simple code to receive SMS and store it in the database. for mobile with just one sim, it works fine. But when I import the second sim into mobile my code did not work. below is my code:

public class SmsReceiver extends BroadcastReceiver {
    public AppDBHelper db;
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("receiveSMS","sms 1");
        Bundle bundle=intent.getExtras();
        if(bundle==null) return;
        Object[] pdus= (Object[]) bundle.get("pdus");
        if(pdus==null) return;
        SmsMessage[] messages=new SmsMessage[pdus.length];//num of messages
        for(int i=0;i<pdus.length;i++){
            if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
                String format=bundle.getString("format");
                messages[i]=SmsMessage.createFromPdu((byte[]) pdus[i],format);
            }else{
                messages[i]=SmsMessage.createFromPdu((byte[]) pdus[i]);
            }
            String messageText=messages[i].getMessageBody().toString();
            messageText=messageText.replaceAll(" ","");
            messageText=messageText.replaceAll("\n","");
            messageText=messageText.replaceAll("\u200E","");
            if(messageText!=null) {
                db.insertSmsCodes(messageText);// insert into database
            }
        }
    }

}

I also add these permissions to manifest file:

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

Where is my wrong?


Solution

  • Try this code this is working fine in all my projects.

    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.telephony.SmsMessage;
    import android.text.TextUtils;
    
    /**
     * Created by KHEMRAJ on 4/11/2017.
     */
    
    public class SmsBroadcastReceiver extends BroadcastReceiver {
    
        public static final String SMS_RECEIVE_ACTION = "android.provider.Telephony.SMS_RECEIVED";
    
        @Override
        public void onReceive(Context context, Intent intent) {
            if (TextUtils.equals(intent.getAction(), SMS_RECEIVE_ACTION)) {
                Bundle data = intent.getExtras();
                if (data != null) {
                    Object[] pdus = (Object[]) data.get("pdus");
                    if (pdus != null) {
                        for (Object pdu : pdus) {
                            SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdu);
                            String messageBody = smsMessage.getMessageBody();
                            if (!TextUtils.isEmpty(messageBody)) {
                                // your stuff here
                            }
                        }
                    }
                }
            }
        }
    }
    

    In your manifest

       <receiver
                android:name=".utils.SmsBroadcastReceiver"
                android:enabled="true">
                <intent-filter android:priority="999">
                    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
                </intent-filter>
            </receiver>
    

    Only this permission is needed for receiving SMS

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

    You are good to go :)