Android 双卡发送短信

场景:

手机中有两张卡时,如何选择由哪张卡发送短信

实现代码:

private void sendMessage() {

        String SEND = "sms_send";
        PendingIntent send_intent = PendingIntent.getBroadcast(this, 0, new Intent(SEND), 0);

        registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {

                switch (getResultCode()) {
                    case Activity.RESULT_OK:
                        //succeed
                        break;
                    default:
                        //failed
                        break;
                }

            }
        }, new IntentFilter(SEND));

        try{
            SmsManager sms = SmsManager.getDefault();

            Class smClass = SmsManager.class;
            //通过反射查到了SmsManager有个叫做mSubId的属性
            Field field = smClass.getDeclaredField("mSubId");
            field.setAccessible(true);
            field.set(sms,1);//0:默认卡1发送;1:默认卡2发送

            sms.sendTextMessage("13168765085", null, "hello", send_intent, null);
        }catch (Exception e) {
            e.printStackTrace();
        }

    }

发送短信权限添加

<uses-permission android:name="android.permission.SEND_SMS">uses-permission>
<uses-permission android:name="android.permission.READ_SMS">uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS">uses-permission>

你可能感兴趣的:(android)