所以我正在研究这个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);
}
}
}
答案 0 :(得分:1)
您没有为show()
内的Toast调用if
。这就是它没有展示的原因。请注意Toast.show()
不阻止一个Toast
将显示在另一个上。{/ p>