避免为使用 SYSTEM_ALERT_WINDOW 的服务检测到屏幕覆盖

Avoid Screen Overlay Detected for service that uses SYSTEM_ALERT_WINDOW

我的应用主要用途是覆盖,覆盖是运行来自服务。

Android安全加的不错"Screen Overlay Detected"

我想在用户尝试更改权限时避免 "Screen Overlay Detected"。所以...我添加了一个 AccessiblityService 来检测:

if ( event.getPackageName().equals("com.google.android.packageinstaller") ){
    stopService(myServiceIntent);
}

但是,即使是现在我也看到这条消息弹出。 (当我的服务停止时...)。

我看到 Twilight 没有问题。

我错过了什么?

p.s。 - 我也尝试构建一个签名的 apk,但看到了完全相同的行为。

看来我已经能够解决这个问题了。

a) stopService 不能保证您的服务会被停止。 如所述 here :

It will not be destroyed until all of these bindings are removed. See > the Service documentation for more details on a service's lifecycle.

b) 我能够通过发送名为 stopSelf() 的意图来终止我的服务。 但是过程 killing/starting 可能会很慢。

c) 最佳分辨率:所以看起来 Android 检查视图可见性。无需终止服务或做任何更复杂的事情。

我目前的做法: - AccessibilityService(已被我的应用程序使用)监控 "com.google.android.packageinstaller" 尽管它可以细化为 class:"com.android.packageinstaller.permission.ui.ManagePermissionsActivity"

  • 一旦在此 class 中检测到,我们就会将 Intent 发送到 "duck",当我们出去时,我们会发送另一个我们重新开始的 Intent。

  • 该服务通过以下方式处理这些调用:

    [ourView].setVisibility(View.INVISIBLE); // when permission settings shown

    [ourView].setVisibility(View.VISIBLE); // when normal flow

只要 Android 6.x 在某些设备上无故显示此 "overlay alert" 是错误的(根据我的分析数据,在 2% 到 5% 的设备上) ,最好的解决方案是通过将 targetSdk 定义为 22 来避免整个权限过程。注意您不能将目标 sdk 降级为新版本,否则当用户更新需要unisntall/install 的应用。