首页 > 解决方案 > 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);



}

标签: javaandroid

解决方案


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);

推荐阅读