我必须使用发送5-10条消息,
SmsManager smsManager= SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo,null,messageTosend,null,null);
但条件是我需要确认每条消息发送成功,以便我可以继续发送其余的消息,依此类推,直到全部发送出去。 我知道如何使用广播接收器侦听成功,但不知道如何暂停进一步的消息发送,直到广播通知。
final BroadcastReceiver sendSMS = new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getContext(), "Generic failure",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getContext(), "No service",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getContext(), "Null PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getContext(), "Radio off",
Toast.LENGTH_SHORT).show();
break;
}
}
};
如何在首次发送短信成功之前暂停sendms代码的执行?
答案 0 :(得分:0)
private boolean isChecked = true;
final BroadcastReceiver sendSMS = new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
while(isChecked == true){
switch (getResultCode()) {
case Activity.RESULT_OK:
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getContext(), "Generic failure",
Toast.LENGTH_SHORT).show();
isChecked = false;
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getContext(), "No service",
Toast.LENGTH_SHORT).show();
isChecked = false;
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getContext(), "Null PDU",
Toast.LENGTH_SHORT).show();
isChecked = false;
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getContext(), "Radio off",
Toast.LENGTH_SHORT).show();
isChecked = false;
break;
}
}
}
};
如果消息正确并且您想要继续,或者如果消息为假,请创建一段时间以作弊。