基础方法(系统默认行为)
Android系统本身并未直接提供通用的双卡短信发送接口,默认情况下SmsManager.sendTextMessage()
会使用系统设置的默认SIM卡发送短信,若需手动选择SIM卡,需结合设备特性及厂商定制API。
android.telecom.SubscriptionManager
getDefaultSmsSubId()
getSlotIndex(int subId)
com.huawei.telephony.TelephonyManager
getSlotCount()
getSimOperatorName(int slotId)
示例代码(小米设备)
// 获取所有订阅号(SIM卡) SubscriptionManager manager = (SubscriptionManager) getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE); List<SubscriptionInfo> subs = manager.getActiveSubscriptionInfoList(); // 选择第一个SIM卡发送短信 if (subs != null && !subs.isEmpty()) { int subId = subs.get(0).getSubscriptionId(); // 获取订阅ID SmsManager sms = SmsManager.getSmsManagerForSubscription(subId); sms.sendTextMessage(phoneNumber, null, message, null, null); }
判断设备双卡状态
通过TelephonyManager
可检测设备是否支持双卡:
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); int phoneCount = tm.getPhoneCount(); // >=2 表示双卡 boolean isSecondSimReady = tm.getSimState(1) == TelephonyManager.SIM_STATE_READY; // 判断第二张卡是否可用
通过Intent调用系统短信界面(间接选择)
若无法直接控制SIM卡,可通过隐式Intent跳转到系统短信应用,部分系统会弹出SIM卡选择框:
Intent intent = new Intent(Intent.ACTION_SENDTO); intent.setData(Uri.parse("smsto:" + phoneNumber)); intent.putExtra("sms_body", message); // 部分厂商会在此界面显示SIM卡选择 startActivity(intent);
注意事项
-
权限要求:
问题2:用户未插入SIM卡时如何处理?
解答:
- 调用
TelephonyManager.getSimState(int slotId)
检查SIM卡状态(SIM_STATE_ABSENT
表示无卡)。 - 提示用户插入SIM卡或选择其他可用卡槽。
- 若所有卡槽均不可用,禁用发送按钮并显示错误
- 调用