Android系统卸载弹窗监听器实现方法咨询
Hey there! Let's figure out how to listen for that uninstall confirmation dialog on Android—since Device Administrator isn't the right tool for this job, I'll walk you through the actual viable solutions.
First, why didn't Device Administrator work? That API is designed for device-level management tasks like remote wipe, disabling cameras, or enforcing security policies. It has no hooks into system UI events like dialog pops, so hitting a wall there makes total sense.
The Most Feasible Approach: Accessibility Service
Android's Accessibility Service is the legitimate way to monitor system UI changes, including the appearance of the uninstall confirmation dialog. Here's how to implement it step by step:
1. Create the Accessibility Service Class
This service will listen for window state changes and scan for the uninstall dialog's text:
public class UninstallDialogListenerService extends AccessibilityService { @Override public void onAccessibilityEvent(AccessibilityEvent event) { // Only react when a new window opens/activates if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) { AccessibilityNodeInfo rootNode = getRootInActiveWindow(); if (rootNode != null && isUninstallDialogPresent(rootNode)) { // Trigger your custom logic here—this means the dialog is showing! Log.d("UninstallListener", "Uninstall confirmation dialog detected!"); // You could also extract the app package name or perform other actions } } } // Recursively scan UI nodes for the uninstall dialog text private boolean isUninstallDialogPresent(AccessibilityNodeInfo node) { if (node == null) return false; // Check if current node has the target dialog text CharSequence nodeText = node.getText(); if (nodeText != null && nodeText.toString().contains("Do you want to uninstall this app?")) { return true; } // Check all child nodes for (int i = 0; i < node.getChildCount(); i++) { if (isUninstallDialogPresent(node.getChild(i))) { return true; } } return false; } @Override public void onInterrupt() { // Handle service interruptions here (e.g., system kills the service) } }
2. Register the Service in Your Manifest
Add this entry to your AndroidManifest.xml—don't forget the required permissions and configuration:
<service android:name=".UninstallDialogListenerService" 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>
3. Create the Accessibility Service Configuration File
Make a new file res/xml/accessibility_service_config.xml to define what the service monitors:
<?xml version="1.0" encoding="utf-8"?> <accessibility-service xmlns:android="http://schemas.android.com/apk/res/android" android:accessibilityEventTypes="typeWindowStateChanged" android:accessibilityFeedbackType="feedbackGeneric" android:accessibilityFlags="flagDefault" android:canRetrieveWindowContent="true" android:description="@string/accessibility_service_description" android:packageNames="com.android.settings, com.android.launcher3" />
- Update
packageNamesto match your device's Settings app and launcher (some devices use different launcher packages likecom.google.android.apps.nexuslauncher). - Add a description string in
strings.xmlexplaining what the service does (required for user consent).
Important Notes
- User Consent Required: Your users will need to manually enable this service in Android Settings > Accessibility. You can prompt them to go there with an intent if needed.
- Localization Considerations: The dialog text varies by language. Instead of hardcoding the English string, you could check for the dialog's package (Settings app) and window type, or use localized strings from your app's resources.
- Play Store Policies: Don't misuse Accessibility Services—Google Play may reject apps that use this API for non-accessibility purposes. Make sure your use case is legitimate.
Alternative: Listen for Uninstall Intents
If you don't need to catch the dialog itself, just want to know when an uninstall is initiated, you can register a broadcast receiver for ACTION_DELETE or ACTION_UNINSTALL_PACKAGE intents. Note that this will only trigger when the uninstall flow starts, not when the confirmation dialog pops up.
内容的提问来源于stack exchange,提问作者Hardik Pandya

