SmsManager sendTextMessage在某些条件下不工作.
我创建了一个使用SmsManager发送消息的android应用程序,它在模拟器和实际设备上一直运行良好。然而,最近我几乎没有收到用户的抱怨,说我的应用程序无法在他们的设备上发送短信。我很确定所有的权限都被授予了,并且在Crashlytics报告中没有崩溃和错误。
我试着在Android模拟器上模拟这个问题,我意识到,当我将蜂窝网络类型更改为"GSM“并将其更改回”完全“时,可能会导致SmsManager sendTextMessage无法工作。我还添加、发送和交付了挂起的意图,但没有一个返回错误代码给我。调用sendTextMessage方法后完全没有响应。
如果您对此有任何解决方案,请帮助。
备注:
public void sendSMS(String phoneNumber, String message) {
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(getContext(), 0, new Intent(
SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(getContext(), 0, new Intent(
DELIVERED), 0);
BroadcastReceiver sendSMS = new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Broadcast_SMSReceiver.mSMSReceiver.receivedSMS(true);
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;
case SmsManager.RESULT_ERROR_SHORT_CODE_NOT_ALLOWED:
Toast.makeText(getContext(), "Premium short codes denied",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_SHORT_CODE_NEVER_ALLOWED:
Toast.makeText(getContext(), "Premium short codes denied",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_LIMIT_EXCEEDED:
Toast.makeText(getContext(), "Reached the sending queue limit",
Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(getContext(), getResultCode(),
Toast.LENGTH_SHORT).show();
}
}
};
BroadcastReceiver deliverSMS = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getContext(), "SMS delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getContext(), "SMS not delivered",
Toast.LENGTH_SHORT).show();
break;
}
}
};
getActivity().registerReceiver(sendSMS, new IntentFilter(SENT));
getActivity().registerReceiver(deliverSMS, new IntentFilter(DELIVERED));
try {
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, "test", sentPI, deliveredPI);
}catch (Exception e){
Toast.makeText(getContext(),
"SMS failed, please try again later ! ",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
Broadcast_SMSReceiver.mSMSReceiver.receivedSMS(true);
}发布于 2018-07-08 13:57:45
我认为您的问题可能是用户想要发送的信息超过160个字符。
看起来,如果您使用超过允许长度的消息调用SmsManager::sendTextMessage(...),则调用将悄然失败。不会引发任何异常,sentPI意图也不会被执行。
https://stackoverflow.com/questions/49366654
复制相似问题