我尝试做的是向另一部手机发送短信,并确认短信是否送达。 现在,只有确认短信已发送才有效。所以我收到消息说我的短信已发送,但我什么也没收到。 我正在使用 2 个模拟器对其进行测试,但我认为这不是问题所在。可惜我暂时没有安卓手机:(
我的代码是这样的:
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.
关于Android:在模拟器中未收到短信发送确认,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9503585/