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;
}
答案 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;
}