Android设备开机后自动输入SIM卡PIN码的编程实现咨询
Alright, let's break down how to automatically fill in the SIM PIN when the system prompts for it right after your Android device finishes booting up. This isn't a trivial task because Android prioritizes security for SIM operations, but there are two main approaches depending on your access level to the device/system.
1. System-Level App/Custom ROM Approach (Full Control)
If you're building a custom ROM or have the ability to sign your app with the system signature, this is the most reliable method. Regular third-party apps can't access the necessary APIs due to strict security restrictions.
Step 1: Listen for SIM State Changes
Instead of only relying onACTION_BOOT_COMPLETED(which might fire before the SIM PIN prompt appears), register a broadcast receiver forandroid.intent.action.SIM_STATE_CHANGED. This will alert you when the SIM enters theSIM_STATE_PIN_REQUIREDstate—exactly when we need to act.Step 2: Use TelephonyManager to Supply the PIN
Once you detect the PIN-required state, use theTelephonyManagerclass'ssupplyPin()method (orsupplyPinForSubscriber()for multi-SIM devices on newer Android versions). This method requires theandroid.permission.MODIFY_PHONE_STATEpermission, which is only granted to system-signed apps.Here's a quick code snippet for the broadcast receiver:
public class SimPinReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String simState = intent.getStringExtra(TelephonyManager.EXTRA_STATE); if (TelephonyManager.SIM_STATE_PIN_REQUIRED.equals(simState)) { TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); // Replace "YOUR_PIN" with your actual SIM PIN boolean success = telephonyManager.supplyPin("YOUR_PIN"); // Handle success/failure cases as needed } } }Don't forget to declare the receiver and permissions in your
AndroidManifest.xml:<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <receiver android:name=".SimPinReceiver"> <intent-filter> <action android:name="android.intent.action.SIM_STATE_CHANGED" /> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver>
2. Accessibility Service Approach (For Regular Apps)
If you can't build a system app, the only feasible workaround is using an Accessibility Service. This lets your app simulate user input, but it requires the user to manually enable the service in Settings—so it's not fully "automatic" out of the box.
Step 1: Create the Accessibility Service
First, define the service in yourAndroidManifest.xmlwith the necessary permissions:<service android:name=".SimPinAccessibilityService" android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"> <intent-filter> <action android:name="android.accessibilityservice.AccessibilityService" /> </intent-filter> <meta-data android:name="android.accessibilityservice" android:resource="@xml/accessibility_service_config" /> </service>Step 2: Configure the Accessibility Service
Create anaccessibility_service_config.xmlfile inres/xml/to specify which windows to monitor (adjust package names for your device's manufacturer):<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android" android:accessibilityEventTypes="typeWindowStateChanged" android:accessibilityFeedbackType="feedbackGeneric" android:accessibilityFlags="flagDefault" android:canPerformGestures="true" android:canRetrieveWindowContent="true" android:description="@string/accessibility_service_description" android:packageNames="com.android.phone, com.android.systemui" />Step 3: Implement the Service Logic
In your service class, overrideonAccessibilityEventto detect the PIN input window and simulate input:public class SimPinAccessibilityService extends AccessibilityService { @Override public void onAccessibilityEvent(AccessibilityEvent event) { if (event.getEventType() != AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) { return; } AccessibilityNodeInfo rootNode = getRootInActiveWindow(); if (rootNode == null) return; // Find the PIN input field (adjust view ID based on your device's UI) List<AccessibilityNodeInfo> inputFields = rootNode.findAccessibilityNodeInfosByViewId("com.android.phone:id/pin_entry"); if (!inputFields.isEmpty()) { AccessibilityNodeInfo inputField = inputFields.get(0); // Replace "YOUR_PIN" with your actual SIM PIN inputField.performAction(AccessibilityNodeInfo.ACTION_SET_TEXT, Bundle.forCharSequence(AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE, "YOUR_PIN")); // Find and click the confirm button List<AccessibilityNodeInfo> confirmButtons = rootNode.findAccessibilityNodeInfosByText("OK"); if (!confirmButtons.isEmpty()) { confirmButtons.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK); } } } @Override public void onInterrupt() { // Handle service interruptions here } }
Key Notes & Limitations
- Security Risks: Auto-filling your SIM PIN removes a critical security layer—if your device is lost or stolen, anyone can access it without entering the PIN.
- Device/ROM Compatibility: The Accessibility Service approach relies on the SIM PIN UI's structure, which varies between Android versions and manufacturers. You'll need to test and adjust view IDs/texts for different devices.
- Permissions: System-level methods require system signing, while Accessibility Services need explicit user consent to enable.
内容的提问来源于stack exchange,提问作者Mohammed Darwish

