BroadcastReceiver不会读取所有短信长度

时间:2019-04-09 07:51:29

标签: android sms

BroadcastReceiver不会读取短信中的所有单词,只是从中读取了一些单词,请问有人可以帮助我吗?

公共类SMSBReceiver扩展了BroadcastReceiver {     私有静态MessageListener mListener;

@Override
public void onReceive(Context context, Intent intent) {
    Bundle data = intent.getExtras();
    if (data != null) {

        Object[] pdus = (Object[]) data.get("pdus");
        for (int i = 0; i < pdus.length; i++) {
            SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdus[i]);

            if (smsMessage.getDisplayOriginatingAddress().toString().equals("Samba.")) {
                String message = smsMessage.getMessageBody().toString();
                mListener.messageReceived(message);
            }
        }
    }
}
public static void bindListener(MessageListener listener){
    mListener = listener;
}

1 个答案:

答案 0 :(得分:0)

我认为您只需要附加所有短信:

更改此:

String message = smsMessage.getMessageBody().toString();





String message += smsMessage.getMessageBody().toString();



因此最终代码将类似于-

@Override
public void onReceive(Context context, Intent intent) {
    Bundle data = intent.getExtras();
    if (data != null) {

        Object[] pdus = (Object[]) data.get("pdus");
String message = "";
        for (int i = 0; i < pdus.length; i++) {
            SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdus[i]);

            if (smsMessage.getDisplayOriginatingAddress().toString().equals("Samba.")) {
                 message += smsMessage.getMessageBody().toString();
                mListener.messageReceived(message);
            }
        }
    }
}
public static void bindListener(MessageListener listener){
    mListener = listener;
}