如何无需申请Overlay权限在其他应用上绘制?以Facebook Messenger为例
Great question! This is a super common point of confusion for Android developers—after all, we’re typically taught to request the SYSTEM_ALERT_WINDOW permission for any overlay UI. Apps like Facebook Messenger get around this by leveraging official Android features and platform exceptions, so let’s break down how it works:
Notification Bubbles (Android 11+)
The chat heads you see in Messenger are built using Android’s native Notification Bubbles API, introduced in Android 11. This is part of the system’s core notification framework, so it doesn’t require theSYSTEM_ALERT_WINDOWpermission at all. As long as your app has the standardPOST_NOTIFICATIONSpermission, you can create these floating bubbles that overlay other apps. It’s Google’s official, permission-compliant way to implement chat-style overlays.Manufacturer Whitelisting
On some Android devices (especially from major OEMs like Samsung, Xiaomi, or Huawei), popular apps like Messenger are added to a system-level whitelist. This grants them default access to overlay permissions without requiring users to manually approve theSYSTEM_ALERT_WINDOWprompt. Note that this is a vendor-specific tweak, not a universal Android behavior.Legacy Workarounds (Older Android Versions)
Before Android 8.0 (API 26), theSYSTEM_ALERT_WINDOWpermission was far less strictly enforced. Apps could use window types likeTYPE_PHONEto draw overlays without explicit permission. However, Google locked this down in newer Android versions, so Messenger now relies entirely on modern, official APIs like Notification Bubbles for its overlay features.Avoiding Accessibility Service Misuse
While some apps abuse Accessibility Services to draw overlays (since these services have broad system access), Messenger doesn’t take this route. Accessibility services are intended for assistive tools, not general UI overlays—using them for this purpose would violate Google Play policies and force users to manually enable the service, creating a poor user experience.
If you’re looking to build a similar feature, stick with Notification Bubbles—it’s the supported, permission-friendly way to create floating UI elements that work across all modern Android devices.
内容的提问来源于stack exchange,提问作者Lokesh

