英文:
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:
<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>
Here is my accessibility service configuration XML file:
<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" />
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("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
}
}
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
将以下内容添加到您在清单中的 <service
声明中:
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>
此外,您可能还需要在您的服务配置 XML 中添加以下行:
android:description="@string/accessibility_service_desc"
android:notificationTimeout="25"
英文:
add this filter to your <service
declaration in manifest
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>
also you may have to add these lines in your service config xml
android:description="@string/accessibility_service_desc"
android:notificationTimeout="25"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论