[英]SmsManager sendTextMessage Not Working
SmsManager sendTextMessage在某些情況下不起作用。
我創建了一個使用SmsManager發送消息的android應用程序,它在模擬器和真實設備上一直運行良好。 但是最近,我收到用戶的抱怨很少,稱我的應用無法在他們的設備上發送短信。 我很確定在Crashlytics報告中授予了所有權限,並且沒有崩潰和錯誤。
我嘗試在Android Emulator上模擬此問題,並且意識到當我將蜂窩網絡類型更改為“ GSM”並將其更改回“ Full”多次時,可能會導致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);
}
我認為您的問題可能出在用戶要發送的消息超過160個字符。
看起來,如果您使用比允許的時間更長的消息來調用SmsManager::sendTextMessage(...)
,則調用將以靜默方式失敗。 沒有拋出異常,並且sendPI意圖也沒有執行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.