问题
Here is my code..
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) {
switch (getResultCode()) {
case Activity.RESULT_OK:
ContentValues values = new ContentValues();
//for (int i = 0; i < MobNumber.size() - 1; i++) {
// values.put("address", MobNumber.get(i).toString());// txtPhoneNo.getText().toString());
// values.put("body", MessageText.getText().toString());
//}
getContentResolver().insert(
Uri.parse("content://sms/sent"), values);
Toast.makeText(getBaseContext(), "SMS sent",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic failure",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No service",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio off",
Toast.LENGTH_SHORT).show();
break;
}
}
}, 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);
}
I am not sending so many SMSs at a time. I am not sending it fast. This code is under activity which extends AppCompactActivity. I have read all the links about this error on this site. I can't even send one sms. My mobile account balance is enough to send sms. My network service is also fine and I can send and receive sms. just getting one error "Generic failure". Please help me if any one have any idea.
回答1:
I think you need to check the message center number. If it is not set or correct than this failure may happen. You will found this number at the SMS settings option.
回答2:
If your message is too long then use handler inside your code
new Handler().postDelayed(new Runnable() {
public void run() {
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}
}, 1000*40);
or use this link
回答3:
If you are using a Dual Sim Phone, please check if Preferred Sim for SMS, is IS NOT set to "Ask every time".
You need to set a default SIM. if you are using
SmsManager sms = SmsManager.getDefault();
回答4:
If your phone is dual sim than first you need to set a preferred sim for sms in your phone setting (google it) and that default sim must be contain some balance amount...the other short trick to set preferred default sim in your phone is-just turn one sim off for some time so another sim will automatically become your preferred default sim for sms :) The phone number parameter in sendTextMessage() can be used in both ways i.e with country code like +919783###### or without code like 9783######. both will work
回答5:
Your message might be too long. In that case, you should use divideMessage
and sendMultipartTextMessage
, like this:
smsManager.sendMultipartTextMessage(phoneNumber, null, divideMessage(message), null, null);
来源:https://stackoverflow.com/questions/36477226/sending-sms-error-generic-failure-again-and-agian