双卡上未收到短信

时间:2018-04-21 16:09:04

标签: android smsmanager

我编写了一个简单的代码来接收SMS并将其存储在数据库中。对于只有一个SIM卡的手机,它运行正常。但是当我将第二个SIM卡导入移动设备时,我的代码无效。下面是我的代码:

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
            }
        }
    }

}

我还将这些权限添加到清单文件中:

    <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" />

我哪里错了?

1 个答案:

答案 0 :(得分:0)

尝试使用此代码,这在我的所有项目中都能正常运行。

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
                        }
                    }
                }
            }
        }
    }
}

在你的清单中

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

接收短信只需要此权限

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

你很高兴:)