java - my accessibility service is not working//
问题描述
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=".MainActivity$MyAccessibilityService"
android:enabled="true"
android:exported="true"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"
android:label="@string/accessibility_service_description">
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>
<meta-data
android:name="android.accessibilityservice"
android:resource="@xml/actions" />
</service>
Here is my accessibility service configuration XML file:
<?xml version ="1.0" encoding ="utf-8"?><!-- Learn More about how to use App Actions: https://developer.android.com/guide/actions/index.html -->
<actions>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:description="@string/accessibility_service_description"
android:notificationTimeout="25"
android:accessibilityEventTypes="typeAllMask"
android:accessibilityFeedbackType="feedbackAllMask"
android:accessibilityFlags="flagDefault"
android:canRequestEnhancedWebAccessibility="true"
android:canRetrieveWindowContent="true"
android:packageNames="com.example.jostick3"
android:canRequestTouchExplorationMode="true"
android:settingsActivity="com.example.jostick3.MainActivity" />
</actions>
Here is my accessibility service class:
public class MyAccessibilityService extends AccessibilityService {
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
final int eventType = event.getEventType();
String eventText = null;
switch(eventType) {
/*
You can use catch other events like touch and focus
case AccessibilityEvent.TYPE_VIEW_CLICKED:
eventText = "Clicked: ";
break;
case AccessibilityEvent.TYPE_VIEW_FOCUSED:
eventText = "Focused: ";
break;
*/
case AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED:
eventText = "Typed: ";
break;
}
eventText = eventText + event.getText();
//print the typed text in the console. Or do anything you want here.
System.out.println("ACCESSIBILITY SERVICE : "+eventText);
textView.setText("ok"+i);i++;
}
@Override
public void onInterrupt() {
//whatever
textView.setText("ok"+i);i++;
}
@Override
public void onServiceConnected() {
//configure our Accessibility service
AccessibilityServiceInfo info=getServiceInfo();
info.eventTypes = AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED;
info.feedbackType = AccessibilityServiceInfo.FEEDBACK_SPOKEN;
info.notificationTimeout = 100;
this.setServiceInfo(info);
textView.setText("ok"+i);i++;
}
}
Lastly, this is the OnCreate method of my MainActivity.java:
public class MainActivity extends AppCompatActivity {
EditText editText;
TextView textView;
public int i=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=findViewById(R.id.testo);
editText=findViewById(R.id.editText1);
Intent openSettings = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
openSettings.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(openSettings);
textView.setText("daaa"+i);
}
解决方案
Did you do something like this:
protected boolean checkAccess() {
String string = getString(R.string.accessibilityservice_id);
for (AccessibilityServiceInfo id : ((AccessibilityManager) getSystemService(Context.ACCESSIBILITY_SERVICE)).getEnabledAccessibilityServiceList(AccessibilityEvent.TYPES_ALL_MASK)) {
if (string.equals(id.getId())) {
return true;
}
}
return false;
}
In order for service to start working, it needs to be granted rights in a special section of settings. To do that you can show user some alert dialog with button wich send him to settings with this code:
Intent intent = new Intent(android.provider.Settings.ACTION_ACCESSIBILITY_SETTINGS);
startActivityForResult(intent, 0);
推荐阅读
- asp.net - ASP.NET 使用 iframe 进行 3ds - 如何返回到托管页面
- python - 更新 config.ini 时 IronPython 包“System.UnauthorizedAccessException”
- python - 自动确定绘图大小 matplotlib
- ruby-on-rails - 控制器“params”在测试环境和开发环境中表现不同
- c# - COM 端口在看似随机的时间变得忙碌
- node.js - 我在 Mongoose 中使用“创建”变得不确定
- vue.js - 从 vuex 和 Vue-native-websocket 插件接收 WebSockets 数据
- reactjs - 部署到 github 后我得到一个空白页面,没有任何错误
- actions-on-google - 是否有可以让我向 Google Assistant 发送命令的 API
- excel - 如何文本框检查自身