4

我正在使用 SMS Manager 类通过我的代码发送 SMS。该代码在单卡手机中运行良好,但在某些双卡手机中,它返回给我 GENERIC FAILURE。我为此搜索了很多,但没有找到合适的解决方案。请帮帮我。我的代码是,

    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);

我的接收器是,

    @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

0 回答 0