问题
I am trying to create a feature in my app which can send SMS to multiple contacts in background. I am using SMS Manager API
to do the same. However, everytime RESULT_ERROR_GENERIC_FAILURE
returns. I read lot of similar posts on stack overflow and tried all the solutions but none of them seems to work for me. To be specific, I have tried following:
- Change phone number format to "+91xxxxxxxxxx", "9xxxxxxxxx", "91xxxxxxxxxx"
- Checked the SIM balance
- SMS Service centre number is also valid
- SMS Message is well within 160 characters
- Delay sending multiple SMS
Here is how I am sending SMS:
for (int index = 0; index < contactsList.size(); index++) {
final String phonenumber = contactsList.get(index).getPhone().substring(1);
if (index == 0) {
sendSMS(phonenumber.trim(), sms, contactsList);
} else {
new Handler().postDelayed(new Runnable() {
public void run() {
sendSMS(phonenumber.trim(), sms, contactsList);
}
}, 1000 * 20);
}
}
Here is sendSMS
method:
private void sendSMS(String phoneNumber, final String message, final List<MyContactsActivity.ContactsRow> list) {
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(context, 0, new Intent(
SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(context, 0,
new Intent(DELIVERED), 0);
// ---when the SMS has been sent---
context.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 < list.size() - 1; i++) {
values.put("address", list.get(i).getPhone());
values.put("body", message);
}
context.getContentResolver().insert(
Uri.parse("content://sms/sent"), values);
Toast.makeText(context, "SMS sent", Toast.LENGTH_SHORT).show();
Log.e("Panic", "1");
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(context, "Generic failure", Toast.LENGTH_SHORT).show();
Log.e("Panic", "2");
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(context, "No service", Toast.LENGTH_SHORT).show();
Log.e("Panic", "3");
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(context, "Null PDU", Toast.LENGTH_SHORT).show();
Log.e("Panic", "4");
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(context, "Radio off", Toast.LENGTH_SHORT).show();
Log.e("Panic", "5");
break;
}
}
}, new IntentFilter(SENT));
// ---when the SMS has been delivered---
context.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(context, "SMS delivered", Toast.LENGTH_SHORT).show();
Log.e("Panic", "6");
break;
case Activity.RESULT_CANCELED:
Toast.makeText(context, "SMS not delivered", Toast.LENGTH_SHORT).show();
Log.e("Panic", "7");
break;
}
}
}, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}
Here are my relevant manifest permissions:
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS" />
NOTE: I am testing my app on real device running Android 6. I have checked runtime permissions as well as they are enabled.
Can anyone please tell me what I am doing wrong??
回答1:
I have found the solution to this problem. In case of dual sim phones, there is an option which specifies from which SIM to send SMS. If option has been set to "Ask everytime", generic failure occurs. Solution is to set this option to either of the SIM card. However, I have not yet found programmatic way to do that.
回答2:
Your code is correct. check you have active SIM card and having balance in it. Because Generic failure is response from your service provider.
来源:https://stackoverflow.com/questions/39932647/smsmanager-returns-result-error-generic-failure-despite-trying-everything