我有一个 BroadcastReceiver,它可以处理 android.provider.Telephony.SMS_RECEIVED
操作,并在某些情况下发送短信作为回复。我的应用程序具有 android.permission.SEND_SMS
,并且在 GSM 手机上运行良好。然而,它不适用于我目前唯一可用的 CDMA 手机,并且我怀疑它只发生在 CDMA 手机上。它适用于我尝试过的所有 GSM 手机,数量相当多。 logcat
不显示任何错误或警告,每次调用 sendSms
时仅显示 D/SMSSMSSMSSMS(260): TeleService: 4098
。此外,我在 Activity 中尝试了完全相同的代码,它工作得很好。
我正在使用的代码:
private void sendSms(String destination, String message) {
if(preferencesManager.smsRepliesEnabled()) {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(destination, null, message, null, null);
}
}
preferencesManager.smsRepliesEnabled()
按预期工作,并且 destination
和 message
设置正确。我添加了一个 Log.d
语句来确认这三个内容。同样,PhoneNumberUtils.isWellFormedSmsAddress(destination)
返回true
。
编辑:根据@wojci的建议,我添加了一个sentIntent
并记录了结果代码。结果代码为 RESULT_ERROR_GENERIC_FAILURE
,额外的 errorCode
设置为 -1
。然而,当我在 Activity 中尝试完全相同的代码时,RESULT_OK
可以正常工作。
最佳答案
您写道,您使用以下方式发送文本: smsManager.sendTextMessage(目的地, null, 消息, null, null);
你为什么不使用 sentIntent参数可以告诉你你的消息是否被网络接受?
来自文档:
sentIntent if not NULL this PendingIntent is broadcast when the message is successfully sent, or failed. The result code will be Activity.RESULT_OK for success, or one of these errors:
RESULT_ERROR_GENERIC_FAILURE
RESULT_ERROR_RADIO_OFF
RESULT_ERROR_NULL_PDU
也许它可以为您提供更多线索,了解为什么您的短信发送功能无法按您的预期进行。
关于java - 来自 BroadcastReceiver 的 SmsManager.sendTextMessage 在 CDMA 手机上不执行任何操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11727515/