我正在尝试使用通常可用的代码发送小于160个字符的SMS。但是对于某些手机而言,我会获得通用的失败。我也给了所有SMS读/写入权限。我尝试了通过电话号码和国家代码没有国家代码的方式,但仍会遇到相同的错误。为什么我会遇到这个错误?以下是代码。
private void sendSMS(String phoneNumber, String message)
{
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
new Intent(DELIVERED), 0);
//---when the SMS has been sent---
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
if(getResultCode()== Activity.RESULT_OK){
Toast.makeText(getBaseContext(), "SMS sent",
Toast.LENGTH_SHORT).show();
} else if (getResultCode()== SmsManager.RESULT_ERROR_GENERIC_FAILURE){
Toast.makeText(getBaseContext(), "Generic failure",
Toast.LENGTH_SHORT).show();
} else if (getResultCode() == SmsManager.RESULT_ERROR_NO_SERVICE){
Toast.makeText(getBaseContext(), "No service",
Toast.LENGTH_SHORT).show();
}else if (getResultCode() == SmsManager.RESULT_ERROR_NULL_PDU){
Toast.makeText(getBaseContext(), "Null PDU",
Toast.LENGTH_SHORT).show();
}else if (getResultCode() == SmsManager.RESULT_ERROR_RADIO_OFF){
Toast.makeText(getBaseContext(), "Radio off",
Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(getBaseContext(), "Unknown error!!",
Toast.LENGTH_SHORT).show();
}
}
}, new IntentFilter(SENT));
//---when the SMS has been delivered---
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS not delivered",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
//ArrayList<String> texts = sms.divideMessage(message);
//sms.sendMultipartTextMessage(phoneNumber, null, texts, null, null);
}
以下是我给予的权限。
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
是这些手机的API上方或等于6.0。如果是这样,您需要明确/编程征求许可。