Android:未在模拟器中接收短信已发送确认
作者:互联网
我尝试做的是将短信发送到另一部手机,并在短信被送达时获得确认.
现在,只确认已发送短信有效.所以我收到消息说我的短信已被发送,但后来我什么都没得到.
我正在使用2个模拟器来测试它,但我不认为这是问题所在.不幸的是,我没有手机与andoid片刻:(
我的代码看起来像这样:
private void sendSms(String txt, String phone){
PendingIntent sentPI = PendingIntent.getBroadcast(act, 0, new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(act, 0, new Intent(DELIVERED), 0);
//---when the SMS has been sent---
act.registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(act, "SMS sent",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(act, "Generic failure",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(act, "No service",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(act, "Null PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(act, "Radio off",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT));
//---when the SMS has been delivered---
act.registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(act, "SMS delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(act, "SMS not delivered",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phone, null, txt, sentPI, deliveredPI);
}
解决方法:
根据“SMS Messaging in Android”-tutorial on mobiforge.com,交付状态报告不适用于模拟器:
When it is successfully delivered, it will display a “SMS delivered” message. Note that for testing using the emulator, when an SMS is successfully delivered, the “SMS delivered” message does not appear; this only works for real devices.
标签:confirmation,android,sms 来源: https://codeday.me/bug/20190826/1730573.html