所以我正在开发这个 Android 应用程序,出于某种原因,我花了几个小时试图修复并且可能是一些愚蠢的错误,但由于某种原因,当它到达 if 语句时代码不起作用。当我发送诸如“你好”之类的消息时,会弹出带有消息长度的吐司消息,并且发件人说 5 和 10,用于 10 位电话号码。但是,当它到达 if 语句时,它的行为就好像它是相等的。任何帮助将非常感激。这是我的代码
public class SmsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String msg_from = null;
String msgBody = null;
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
Bundle bundle = intent.getExtras(); //---get the SMS message passed in---
SmsMessage[] msgs = null;
if (bundle != null){
// Toast.makeText(context, "SMS Received", Toast.LENGTH_LONG).show();
//---retrieve the SMS message received---
try{
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for(int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
msg_from = msgs[i].getOriginatingAddress();
msgBody = msgs[i].getMessageBody();
msgBody = msgBody.trim();
msg_from = msg_from.trim();
}
}catch(Exception e){
Log.d("Exception caught",e.getMessage());
}
}
}
Toast.makeText(context, "MsgBody length: " + msgBody.length() + "msg_from length: " + msg_from.length(), Toast.LENGTH_LONG).show();
if (msgBody.length() == 5){
Toast.makeText(context, "Working", Toast.LENGTH_LONG);
}
}
}