0

我卡了很长时间,无法解决。请帮助...我正在尝试从 android 设备以编程方式发送短信。我可以从许多设备成功发送,但不能从一些 android api 级别> = 22 的三星双卡设备发送。对于 api 级别 >=22,我编写了如下代码,

public static final String SMS_SENT_ACTION = "package.name.app.SMS_SENT_ACTION";

registerReceiver(new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        unregisterReceiver(this);
        String message = null;
        switch (getResultCode()) {
            case Activity.RESULT_OK:
                message = "Message Sent Successfully !";
                break;
            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                message = "Error.";
                sendDefaultMessage(default_number, default_send_number, text);
                break;
            case SmsManager.RESULT_ERROR_NO_SERVICE:
                message = "Error: No service.";
                sendDefaultMessage(default_number, default_send_number, text);
                break;
            case SmsManager.RESULT_ERROR_NULL_PDU:
                message = "Error: Null PDU.";
                sendDefaultMessage(default_number, default_send_number, text);
                break;
            case SmsManager.RESULT_ERROR_RADIO_OFF:
                message = "Error: Radio off.";
                sendDefaultMessage(default_number, default_send_number, text);
                break;
        }

        Toast.makeText(GetAppointment.this, message, Toast.LENGTH_LONG).show();

    }
}, new IntentFilter(SMS_SENT_ACTION));


SmsManager.getSmsManagerForSubscriptionId(subscription_id).sendTextMessage(send_number, null, messageToSend, PendingIntent.getBroadcast(
                    this, 0, new Intent(SMS_SENT_ACTION), 0), null);

没有权限相关的问题,因为短信可以从 api 级别 >=22 的华硕双卡设备和索尼单卡设备发送。

我怎样才能使它适用于所有设备?我非常需要帮助。

4

2 回答 2

0

您需要在sendTextMessage()第二个参数中提供要从中发送 SMS 的 SIM 卡的服务中心号码,例如,

SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, "+9198********", msg, null, null);
于 2017-01-04T05:07:08.833 回答
0

然后使用以下功能检查 sim 设置:

public int  getDefaultSimmm(Context context) {

Object tm = context.getSystemService(Context.TELEPHONY_SERVICE);
Method method_getDefaultSim;
int defaultSimm = -1;
try {
    method_getDefaultSim = tm.getClass().getDeclaredMethod("getDefaultSim");
    method_getDefaultSim.setAccessible(true);
    defaultSimm = (Integer) method_getDefaultSim.invoke(tm);
} catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Method method_getSmsDefaultSim;
int smsDefaultSim = -1;
try {
    method_getSmsDefaultSim = tm.getClass().getDeclaredMethod("getSmsDefaultSim");
    smsDefaultSim = (Integer) method_getSmsDefaultSim.invoke(tm);
} catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
return smsDefaultSim;
}

然后以下函数发送短信:

public boolean sendSMS(Context ctx, int simID, String toNum, String centerNum, String smsText, PendingIntent sentIntent, PendingIntent deliveryIntent) {
    String name;

    try {
        if (simID == 0) {
            name = "isms";
            // for model : "Philips T939" name = "isms0"
        } else if (simID == 1) {
            name = "isms2";
        } else {
            throw new Exception("can not get service which for sim '" + simID + "', only 0,1 accepted as values");
        }
        Method method = Class.forName("android.os.ServiceManager").getDeclaredMethod("getService", String.class);
        method.setAccessible(true);
        Object param = method.invoke(null, name);

        method = Class.forName("com.android.internal.telephony.ISms$Stub").getDeclaredMethod("asInterface", IBinder.class);
        method.setAccessible(true);
        Object stubObj = method.invoke(null, param);
        if (Build.VERSION.SDK_INT < 18) {
            method = stubObj.getClass().getMethod("sendText", String.class, String.class, String.class, PendingIntent.class, PendingIntent.class);
            method.invoke(stubObj, toNum, centerNum, smsText, sentIntent, deliveryIntent);
        } else {
            method = stubObj.getClass().getMethod("sendText", String.class, String.class, String.class, String.class, PendingIntent.class, PendingIntent.class);
            method.invoke(stubObj, ctx.getPackageName(), toNum, centerNum, smsText, sentIntent, deliveryIntent);
        }

        return true;
    } catch (ClassNotFoundException e) {
        Log.e("apipas", "ClassNotFoundException:" + e.getMessage());
    } catch (NoSuchMethodException e) {
        Log.e("apipas", "NoSuchMethodException:" + e.getMessage());
    } catch (InvocationTargetException e) {
        Log.e("apipas", "InvocationTargetException:" + e.getMessage());
    } catch (IllegalAccessException e) {
        Log.e("apipas", "IllegalAccessException:" + e.getMessage());
    } catch (Exception e) {
        Log.e("apipas", "Exception:" + e.getMessage());
    }
    return false;
}

我希望它会帮助你

于 2017-01-04T05:13:32.173 回答