双卡设备无法接收SMS问题咨询(附单卡正常实现代码)
Hey there! Let's figure out why your SMS receiver stops working on dual-SIM devices. I've run into this exact issue before, so here's what's going on and how to fix it:
Common Issues with Dual-SIM SMS Receivers
Your single-SIM code works because it doesn't need to account for multiple SIM slots, but dual-SIM devices require extra handling for two key reasons:
- The SMS broadcast might be intercepted by higher-priority receivers or system apps
- Your code doesn't explicitly handle messages from both SIM slots
- Android 10+ imposes stricter restrictions on static broadcast receivers for SMS
Fixed Code with Dual-SIM Support
Here's an updated version of your SmsReceiver that handles both SIM slots and includes critical logging to debug issues:
public class SmsReceiver extends BroadcastReceiver { private AppDBHelper db; @Override public void onReceive(Context context, Intent intent) { Log.i("receiveSMS", "SMS broadcast triggered"); Bundle bundle = intent.getExtras(); if (bundle == null) return; // Get SIM slot ID (critical for dual-SIM devices) int slotId = intent.getIntExtra("slot", -1); Log.i("receiveSMS", "Received message from SIM slot: " + slotId); Object[] pdus = (Object[]) bundle.get("pdus"); if (pdus == null) return; db = new AppDBHelper(context); for (Object pdu : pdus) { SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdu); if (smsMessage == null) continue; String sender = smsMessage.getDisplayOriginatingAddress(); String messageBody = smsMessage.getMessageBody(); long timestamp = smsMessage.getTimestampMillis(); // Store SMS with SIM slot ID for dual-SIM tracking db.insertSms(sender, messageBody, timestamp, slotId); Log.i("receiveSMS", "Saved SMS from " + sender + " (SIM " + slotId + ")"); } db.close(); } }
Update Your Manifest & Permissions
Make sure your receiver has high priority and correct permissions to beat other apps to the broadcast:
<!-- Declare permissions first --> <uses-permission android:name="android.permission.RECEIVE_SMS" /> <uses-permission android:name="android.permission.READ_SMS" /> <!-- Configure the receiver with high priority --> <receiver android:name=".SmsReceiver" android:enabled="true" android:exported="true" android:priority="1000"> <!-- High priority ensures your app gets the broadcast first --> <intent-filter> <action android:name="android.provider.Telephony.SMS_RECEIVED" /> </intent-filter> </receiver>
Android 10+ Critical Fix
On Android 10 (API 29) and above, static broadcast receivers for SMS are restricted unless your app is the default SMS app. If you don't want to set your app as default, switch to dynamic registration:
// Add this to your main Activity or Service private SmsReceiver smsReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Request SMS permissions first (required for Android 6.0+) requestSmsPermissions(); // Dynamically register the receiver smsReceiver = new SmsReceiver(); IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED"); filter.setPriority(1000); registerReceiver(smsReceiver, filter); } @Override protected void onDestroy() { super.onDestroy(); // Don't forget to unregister to avoid memory leaks if (smsReceiver != null) { unregisterReceiver(smsReceiver); } } // Handle runtime permission request for Android 6.0+ private static final int REQUEST_SMS_PERMISSIONS = 100; private void requestSmsPermissions() { String[] permissions = {Manifest.permission.RECEIVE_SMS, Manifest.permission.READ_SMS}; if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECEIVE_SMS) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, permissions, REQUEST_SMS_PERMISSIONS); } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == REQUEST_SMS_PERMISSIONS) { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { Log.i("Permission", "SMS permissions granted - receiver ready"); } else { Log.i("Permission", "SMS permissions denied - receiver won't work"); } } }
Testing Tips
- Send test SMS to both SIM cards and check Logcat for the
receiveSMStags - Verify that no other SMS apps are set as default (they can intercept broadcasts)
- Ensure your app has the required runtime permissions enabled in Settings
内容的提问来源于stack exchange,提问作者BeginnerProgrammer

