SMS GENERIC FAILURE on Dual Sim Android Phones

I am using the SMS manager class to send SMS via my code. The code works well on one SIM phone, but on some two SIM phones it returns GENERIC FAILURE to me. I searched a lot for this, but did not find a suitable solution for this. Please help me. my code is equal

    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(
            "MY_SMS_SENT"), 0);
    PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
            new Intent("MY_SMS_DELIVERED"), 0);
    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, messageBody, sentPI, deliveredPI);

and my receiver is equal

    @Override
public void onReceive(Context context, Intent intent) {
    //errorCode
    if(intent.getAction().equals("MY_SMS_SENT")) {
        switch (getResultCode()) {
        case Activity.RESULT_OK:
            Toast.makeText(context, "SMS sent",
                    Toast.LENGTH_SHORT).show();
            break;
        case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
            TelephonyInfo telephonyInfo = TelephonyInfo.getInstance(context);
            boolean isSIM1Ready = telephonyInfo.isSIM1Ready();
            boolean isSIM2Ready = telephonyInfo.isSIM2Ready();
            boolean isDualSIM = telephonyInfo.isDualSIM();

            Log.e("IS DUAL SIM", "" + isDualSIM);
            Log.e("isSIM1Ready", "" + isSIM1Ready);
            Log.e("isSIM2Ready", "" + isSIM2Ready);

            if(isDualSIM) {
                if(isSIM1Ready || isSIM2Ready) {
                    Toast.makeText(context, "An Unexpected failure occured while sending SMS. Please check whether you have working SMS plan and try again later.",
                            Toast.LENGTH_LONG).show();
                }
                else {
                    Toast.makeText(context, "Please activate atleast one SIM for sending SMS and retry.",
                            Toast.LENGTH_LONG).show();
                }
            }
            else {
                if(!isSIM1Ready) {
                    Toast.makeText(context, "No SIM detected to perform this action.",
                            Toast.LENGTH_LONG).show();
                }
                else {
                    Toast.makeText(context, "An Unexpected failure occured while sending SMS. Please check whether you have working SMS plan and try again later.",
                            Toast.LENGTH_LONG).show();
                }
            }

            break;
        case SmsManager.RESULT_ERROR_NO_SERVICE:
            Toast.makeText(context, "No service available. Please try again later.",
                    Toast.LENGTH_SHORT).show();
            break;
        case SmsManager.RESULT_ERROR_NULL_PDU:
            Toast.makeText(context, "SMS not sent. Please try again later.",
                    Toast.LENGTH_SHORT).show();
            break;
        case SmsManager.RESULT_ERROR_RADIO_OFF:
            Toast.makeText(context, "SMS Not Sent. Please try again later.",
                    Toast.LENGTH_SHORT).show();
            break;
        }
    }
    else if(intent.getAction().equals("MY_SMS_DELIVERED")) {
        switch (getResultCode()) {
        case Activity.RESULT_OK:
            Log.d("SMS STATUS", "SMS delivered");
            /* Toast.makeText(getBaseContext(), "SMS delivered",
                    Toast.LENGTH_SHORT).show();*/
            break;
        case Activity.RESULT_CANCELED:
            Log.d("SMS STATUS", "SMS not delivered");
            /*Toast.makeText(getBaseContext(), "SMS not delivered",
                    Toast.LENGTH_SHORT).show();*/
            break;
        }
    }
}
+4
source share

Source: https://habr.com/ru/post/1542741/


All Articles