Android 后台应用程序能否监听当前forground应用程序显示的内容

Android 后台应用程序能否监听当前forground应用程序显示的内容,android,Android,在android应用程序中,后台应用程序能否监听当前forground应用程序显示的内容?或者至少检查当前是否正在显示特定的其他应用程序 我有一个带有浮动视图的应用程序,它将一个按钮显示为覆盖。 我希望该按钮仅在当前显示的主应用程序是特定应用程序时可见。 我如何检测该应用程序何时打开和何时关闭(或切换到另一个应用程序)?@Adrien NGUYEN您需要在android中使用易访问性服务,您可以执行以下操作: override fun onServiceConnected() { inf

在android应用程序中,后台应用程序能否监听当前forground应用程序显示的内容?或者至少检查当前是否正在显示特定的其他应用程序

我有一个带有浮动视图的应用程序,它将一个按钮显示为覆盖。 我希望该按钮仅在当前显示的主应用程序是特定应用程序时可见。
我如何检测该应用程序何时打开和何时关闭(或切换到另一个应用程序)?

@Adrien NGUYEN您需要在android中使用易访问性服务,您可以执行以下操作:

override fun onServiceConnected() {
    info.apply {
        // Set the type of events that this service wants to listen to. Others
        // won't be passed to this service.
        eventTypes = AccessibilityEvent.TYPE_VIEW_CLICKED or AccessibilityEvent.TYPE_VIEW_FOCUSED

        // If you only want this service to work with specific applications, set their
        // package names here. Otherwise, when the service is activated, it will listen
        // to events from all applications.
        packageNames = arrayOf("com.example.android.myFirstApp", "com.example.android.mySecondApp")

        // Set the type of feedback your service will provide.
        feedbackType = AccessibilityServiceInfo.FEEDBACK_SPOKEN

        // Default services are invoked only if no package-specific ones are present
        // for the type of AccessibilityEvent generated. This service *is*
        // application-specific, so the flag isn't necessary. If this was a
        // general-purpose service, it would be worth considering setting the
        // DEFAULT flag.

        // flags = AccessibilityServiceInfo.DEFAULT;

        notificationTimeout = 100
    }

    this.serviceInfo = info

}
步骤1:创建服务

导入android.accessibilityservice.accessibilityservice 导入android.view.accessibility.AccessibilityEvent

class MyAccessibilityService : AccessibilityService() {

    override fun onInterrupt() {}

    override fun onAccessibilityEvent(event: AccessibilityEvent?) {}

}
步骤2:在清单下声明权限和服务

<service android:name=".MyAccessibilityService"
        android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"
        android:label="@string/accessibility_service_label">
      <intent-filter>
        <action android:name="android.accessibilityservice.AccessibilityService" />
      </intent-filter>
    </service>
给你!您可以继续您的业务逻辑,如显示浮动气泡按钮(及其单击操作)或任何其他功能

您甚至可以使用
[AccessibilityEvent][1]


如果您需要更多信息,请务必告诉我。

Adrien,如果我理解正确,您希望在打开特定应用程序时显示浮动按钮,对吗?我以前做过一个类似的功能,比如当用户打开购物应用程序时显示一个按钮覆盖,这样我们就可以显示特定于应用程序和产品的一些优惠。如果您的功能类似于上述内容,那么您需要向drawer请求其他应用的权限,然后需要编写一些代码,一旦您确认,我将与您共享。这是类似的。到目前为止,我的按钮是一个浮动图标,显示在所有东西的顶部,当我点击它时会触发巫婆代码。我缺少的是使它仅在目标应用程序是当前应用程序时显示,并在关闭或切换到其他应用程序时隐藏。好的。几年前我解决的一个用例。。让我为您找到该代码。尝试将Kotlin转换为java,但似乎有些问题无法正常工作,我选择了
AccessibilityServiceInfo=this.getServiceInfo();info.eventTypes=TYPES\u ALL\u MASK;此.setServiceInfo(信息)如果我设置了
info.eventTypes=TYPES\u ALL\u MASK;//-1
我正在正确接收所有事件,因此我可以对它们执行我想要的操作。但是如果我设置
info.eventTypes=AccessibilityEvent.TYPE\u VIEW\u单击或AccessibilityEvent.TYPE\u VIEW\u FOCUSED//1(0001)或8(1000)>9(1001)
我会停止获取任何事件吗?
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
    android:description="@string/accessibility_service_description"
    android:packageNames="com.example.android.apis"
    android:accessibilityEventTypes="typeAllMask"
    android:accessibilityFlags="flagDefault"
    android:accessibilityFeedbackType="feedbackSpoken"
    android:notificationTimeout="100"
    android:canRetrieveWindowContent="true"
    android:settingsActivity="com.example.android.accessibility.ServiceSettingsActivity"
/>
override fun onServiceConnected() {
    info.apply {
        // Set the type of events that this service wants to listen to. Others
        // won't be passed to this service.
        eventTypes = AccessibilityEvent.TYPE_VIEW_CLICKED or AccessibilityEvent.TYPE_VIEW_FOCUSED

        // If you only want this service to work with specific applications, set their
        // package names here. Otherwise, when the service is activated, it will listen
        // to events from all applications.
        packageNames = arrayOf("com.example.android.myFirstApp", "com.example.android.mySecondApp")

        // Set the type of feedback your service will provide.
        feedbackType = AccessibilityServiceInfo.FEEDBACK_SPOKEN

        // Default services are invoked only if no package-specific ones are present
        // for the type of AccessibilityEvent generated. This service *is*
        // application-specific, so the flag isn't necessary. If this was a
        // general-purpose service, it would be worth considering setting the
        // DEFAULT flag.

        // flags = AccessibilityServiceInfo.DEFAULT;

        notificationTimeout = 100
    }

    this.serviceInfo = info

}