双SIM卡手机APP内选择SIM发送SMS的实现方案求助
Hey there! I’ve run into this exact problem when building an SMS-focused app, so let me break down the solution step by step—this should get you up and running with SIM selection before sending texts.
First, Understand the API Background
Before Android 5.1 (API Level 22), there was no official support for dual-SIM SMS control. So if your app targets devices below this version, you’ll have to inform users that SIM selection isn’t available. For API 22+, Google introduced the SubscriptionManager class to handle multi-SIM operations, which is what we’ll rely on here.
Step 1: Request Necessary Permissions
You’ll need these permissions in your AndroidManifest.xml:
<uses-permission android:name="android.permission.SEND_SMS" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <!-- For Android 10+, use READ_PHONE_NUMBERS instead of READ_PHONE_STATE --> <uses-permission android:name="android.permission.READ_PHONE_NUMBERS" android:maxSdkVersion="29" />
Don’t forget to request these permissions at runtime, especially for Android 6.0 (API 23) and above—users need to explicitly grant access to phone state and SMS features.
Step 2: Fetch Available Active SIM Cards
Use SubscriptionManager to get a list of active SIMs with their details (like carrier name, phone number, and unique subscription ID). Here’s a code snippet:
private List<SubscriptionInfo> getActiveSIMs() { SubscriptionManager subscriptionManager = getSystemService(SubscriptionManager.class); List<SubscriptionInfo> activeSubscriptions = new ArrayList<>(); if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) { activeSubscriptions = subscriptionManager.getActiveSubscriptionInfoList(); } return activeSubscriptions; }
This will give you a list of SubscriptionInfo objects—each represents an active, usable SIM card in the device.
Step 3: Show a SIM Selection Dialog
Once you have the list, create an alert dialog to let users pick their preferred SIM. Here’s how to build it:
private void showSIMSelectionDialog(List<SubscriptionInfo> simList) { if (simList.isEmpty()) { Toast.makeText(this, "No active SIM cards found", Toast.LENGTH_SHORT).show(); return; } if (simList.size() == 1) { // Only one SIM available—send directly without selection sendSMSWithSIM(simList.get(0).getSubscriptionId()); return; } String[] simOptions = new String[simList.size()]; for (int i = 0; i < simList.size(); i++) { SubscriptionInfo sim = simList.get(i); simOptions[i] = sim.getCarrierName() + " (" + sim.getNumber() + ")"; } AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Choose SIM to Send SMS") .setItems(simOptions, (dialog, which) -> { int selectedSubId = simList.get(which).getSubscriptionId(); sendSMSWithSIM(selectedSubId); }) .show(); }
This dialog displays each SIM’s carrier name and associated phone number for clear, user-friendly selection.
Step 4: Send SMS Using the Selected SIM
Use SMSManager.getSMSManagerForSubscriptionId() to get an instance tied to the user’s chosen SIM, then send your message:
private void sendSMSWithSIM(int subId) { String recipientNumber = "RECIPIENT_PHONE_NUMBER"; // Replace with actual number String smsContent = "Your message content here"; try { SMSManager smsManager = SMSManager.getSMSManagerForSubscriptionId(subId); smsManager.sendTextMessage(recipientNumber, null, smsContent, null, null); Toast.makeText(this, "SMS sent successfully", Toast.LENGTH_SHORT).show(); } catch (Exception e) { Toast.makeText(this, "Failed to send SMS: " + e.getMessage(), Toast.LENGTH_SHORT).show(); e.printStackTrace(); } }
Key Notes & Edge Cases
- Permissions Check: Always verify you have the required permissions before accessing
SubscriptionManager—otherwise, you’ll hit aSecurityException. - API Compatibility: For devices below API 22, you can’t specify a SIM—fall back to the system default, or show a message telling users SIM selection isn’t supported on their device.
- Carrier Restrictions: Some carriers may block SMS from certain SIMs (e.g., data-only SIMs), so make sure to handle exceptions gracefully.
Putting it all together: When the user triggers an SMS send, call getActiveSIMs(), then pass the result to showSIMSelectionDialog() to let them pick their SIM before sending.
内容的提问来源于stack exchange,提问作者Samrat Lahiri




