为什么我的辅助功能服务没有被启动?

huangapple go评论78阅读模式
英文:

Why is my accessibility service not being started?

问题

我已创建一个辅助功能服务,以查看是否可以在我的应用中执行手势导航。问题是我的辅助功能服务根本没有启动。关于这个问题的一个可能的提示是,我在设置页面的辅助功能部分看不到我的应用。我已在清单文件中添加了适当的权限,并且不知道我哪里出错了。

这是我的清单文件:

<service 
    android:name=".AccessibilityService"
    android:enabled="true"
    android:exported="true"
    android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"
    android:label="@string/accessibility_service_label">
    <meta-data
        android:name="android.accessibilityservice"
        android:resource="@xml/accessibility_service_config" />
</service>

这是我的辅助功能服务配置 XML 文件:

<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
    android:accessibilityEventTypes="typeAllMask"
    android:accessibilityFeedbackType="feedbackAllMask"
    android:accessibilityFlags="flagDefault"
    android:canRequestEnhancedWebAccessibility="true"
    android:canRetrieveWindowContent="true"
    android:packageNames="com.example.adtry3"
    android:canRequestTouchExplorationMode="true"
    android:settingsActivity="com.example.adtry3.MainActivity" />

这是我的辅助功能服务类:

package com.example.adtry3;

import android.accessibilityservice.AccessibilityServiceInfo;
import android.annotation.TargetApi;
import android.os.Build;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;
import androidx.annotation.RequiresApi;
import java.util.List;

@TargetApi(Build.VERSION_CODES.DONUT)
public class AccessibilityService extends android.accessibilityservice.AccessibilityService {

    @Override
    public void onServiceConnected(){           
        System.out.println("ONSERVICECONNECTED");
        
        AccessibilityServiceInfo info = getServiceInfo();
        
        info.feedbackType = AccessibilityServiceInfo.FEEDBACK_ALL_MASK;
        info.eventTypes = AccessibilityEvent.TYPES_ALL_MASK;
        info.packageNames = new String[] {"com.example.adtry3"};
        info.notificationTimeout = 100;
        
        setServiceInfo(info);
    }
    
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) { 
       System.out.println("Testing");
    }

    @Override
    public void onInterrupt() {

    }

    public void onUnbind(){         //service is being terminated, do final one-time operations here

    }
}

最后,这是我的 MainActivity.java 的 OnCreate 方法:

@Override
protected void onCreate(Bundle savedInstanceState) {
    Intent intent = new Intent(android.provider.Settings.ACTION_ACCESSIBILITY_SETTINGS);
    startActivityForResult(intent, 0);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

如果我漏掉了什么,或者您知道发生了什么,请告诉我。我在其他 StackOverflow 的答案中进行了搜索,但没有成功。谢谢。

英文:

I have created an accessibility service to see if I can perform gesture navigation with my app. The problem is my accessibility service is not being started at all. A possible hint into this problem is that I do not see my app in the accessibility portion of the settings page. I have added the proper permission in my Manifest file, and do not see where I am going wrong.

Here is my Manifest file:

&lt;service 
            android:name=&quot;.AccessibilityService&quot;
            android:enabled=&quot;true&quot;
            android:exported=&quot;true&quot;
            android:permission=&quot;android.permission.BIND_ACCESSIBILITY_SERVICE&quot;
            android:label=&quot;@string/accessibility_service_label&quot;&gt;
            &lt;meta-data
                android:name=&quot;android.accessibilityservice&quot;
                android:resource=&quot;@xml/accessibility_service_config&quot; /&gt;
&lt;/service&gt;

Here is my accessibility service configuration XML file:

&lt;accessibility-service xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    android:accessibilityEventTypes=&quot;typeAllMask&quot;
    android:accessibilityFeedbackType=&quot;feedbackAllMask&quot;
    android:accessibilityFlags=&quot;flagDefault&quot;
    android:canRequestEnhancedWebAccessibility=&quot;true&quot;
    android:canRetrieveWindowContent=&quot;true&quot;
    android:packageNames=&quot;com.example.adtry3&quot;
    android:canRequestTouchExplorationMode=&quot;true&quot;
    android:settingsActivity=&quot;com.example.adtry3.MainActivity&quot; /&gt;

Here is my accessibility service class:

package com.example.adtry3;

import android.accessibilityservice.AccessibilityServiceInfo;
import android.annotation.TargetApi;
import android.os.Build;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;
import androidx.annotation.RequiresApi;
import java.util.List;



@TargetApi(Build.VERSION_CODES.DONUT)
public class AccessibilityService extends android.accessibilityservice.AccessibilityService {


    @Override
    public void onServiceConnected(){           
        System.out.println(&quot;ONSERVICECONNECTED&quot;);
        
        AccessibilityServiceInfo info = getServiceInfo();
        
        info.feedbackType = AccessibilityServiceInfo.FEEDBACK_ALL_MASK;
        info.eventTypes = AccessibilityEvent.TYPES_ALL_MASK;
        info.packageNames = new String[] {&quot;com.example.adtry3&quot;};
        info.notificationTimeout = 100;
        
        setServiceInfo(info);
    }
    
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) { 
       System.out.println(&quot;Testing&quot;);
    }



    @Override
    public void onInterrupt() {

    }


    public void onUnbind(){         //service is being terminated, do final one-time operations here

    }

}

Lastly, this is the OnCreate method of my MainActivity.java:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        Intent intent = new Intent(android.provider.Settings.ACTION_ACCESSIBILITY_SETTINGS);
        startActivityForResult(intent, 0);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
}

If I am missing something, or if you know what is going on, please let me know. I have search on other StackOverflow answers and have had no luck. Thank you.

答案1

得分: 1

将以下内容添加到您在清单中的 &lt;service 声明中:

&lt;intent-filter&gt;
    &lt;action android:name=&quot;android.accessibilityservice.AccessibilityService&quot; /&gt;
&lt;/intent-filter&gt;

此外,您可能还需要在您的服务配置 XML 中添加以下行:

android:description=&quot;@string/accessibility_service_desc&quot;
android:notificationTimeout=&quot;25&quot;
英文:

add this filter to your &lt;service declaration in manifest

&lt;intent-filter&gt;
    &lt;action android:name=&quot;android.accessibilityservice.AccessibilityService&quot; /&gt;
&lt;/intent-filter&gt;

also you may have to add these lines in your service config xml

android:description=&quot;@string/accessibility_service_desc&quot;
android:notificationTimeout=&quot;25&quot;

huangapple
  • 本文由 发表于 2020年10月20日 20:32:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/64445251.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定