Android 12系统下Migraine Relief: Green Dimmer应用悬浮窗导致底层内容无法点击的问题求助
Hey there, let's tackle this issue you're facing with your Migraine Relief: Green Dimmer app on Android 12+. The problem you described—being unable to interact with your device after minimizing the app—usually stems from changes in how Android 12+ handles TYPE_APPLICATION_OVERLAY windows, especially around touch event routing and window lifecycle constraints.
Root Cause Analysis
Your current flag combination (FLAG_NOT_TOUCH_MODAL | FLAG_NOT_TOUCHABLE | FLAG_NOT_FOCUSABLE) should theoretically make the floating window ignore touch events, but Android 12 (API 31) introduced stricter rules for overlay windows. Full-screen overlays can sometimes inadvertently intercept touch input even with these flags, especially when the app moves to the background.
Step-by-Step Fixes
1. Adjust WindowManager Layout Parameters
First, update your window parameters to align with Android 12+ requirements. We'll tweak the flags and add some API-specific settings to ensure the window doesn't block touch events:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { params = new WindowManager.LayoutParams( WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, // Simplify and prioritize flags that prevent touch interception WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, PixelFormat.TRANSLUCENT ); // Add Android 12+ specific adjustments if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { // Disable inset handling to avoid unexpected touch area conflicts params.setFitInsetsTypes(0); // Optional: Prevent screen recording from capturing the overlay if needed params.flags |= WindowManager.LayoutParams.FLAG_SECURE; } }
FLAG_LAYOUT_NO_LIMITSensures the window properly covers the entire screen without being constrained by system UI boundaries, which can prevent unintended touch interception.setFitInsetsTypes(0)tells the system not to adjust the window for system bars, reducing the chance of touch event misrouting.
2. Manage Window Lifecycle with App State
Android 12+ restricts overlay window behavior when the hosting app is in the background. To avoid the window blocking input, update its state when your app moves to the background:
Option A: Update window flags when the app pauses
@Override protected void onPause() { super.onPause(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && params != null && floatingView != null) { // Explicitly enforce the window is not touchable when in background params.flags |= WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE; windowManager.updateViewLayout(floatingView, params); } }
Option B: Remove the window when the app stops, re-add when it resumes
If you don't need the overlay to stay active when the app is minimized, this is a more reliable approach:
private WindowManager windowManager; private View floatingView; @Override protected void onStop() { super.onStop(); if (floatingView != null && windowManager != null) { windowManager.removeView(floatingView); floatingView = null; } } @Override protected void onStart() { super.onStart(); if (floatingView == null && hasOverlayPermission()) { // Recreate and attach the floating window createFloatingWindow(); } } // Helper method to check overlay permission private boolean hasOverlayPermission() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { return Settings.canDrawOverlays(this); } return true; }
3. Verify Overlay Permissions
Double-check that your app properly requests the SYSTEM_ALERT_WINDOW permission and that users have granted it. Even if the app works initially, Android 12+ may revoke implicit access when the app moves to the background.
Why This Works
These changes ensure your green overlay window behaves as a non-interactive layer, even on Android 12+. By aligning with the platform's updated overlay rules and managing the window's lifecycle alongside your app, you prevent the system from treating the overlay as a blocking interactive element.
内容的提问来源于stack exchange,提问作者Bhavin




