即使在 vivo 和 oppo 设备中授予自动启动权限后,AccessibilityService 也会被禁用

AccessibilityService getting disable even after giving autostart permission in vivo and oppo devices

每次在 vivo、oppo 设备中授予自动启动权限后,AccessibilityService 会在一段时间后自动禁用。

Manifest.xml

   <service
        android:name=".Service.MyAccessibilityService"
        android:label="@string/app_name"
        android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">

        <intent-filter>
            <action android:name="android.accessibilityservice.AccessibilityService" />
        </intent-filter>

        <meta-data
            android:name="android.accessibilityservice"
            android:resource="@xml/accessibility_service_config" />

    </service>

accessibility_service_config.xml

<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeAllMask"
android:accessibilityFlags="flagDefault"
android:accessibilityFeedbackType="feedbackGeneric"
android:notificationTimeout="100"
android:canRetrieveWindowContent="true"
android:canRequestFilterKeyEvents="true"
android:settingsActivity="" />

MyAccessibilityService.class

public class MyAccessibilityService 扩展了 AccessibilityService {

 @SuppressLint("WrongConstant")
 @Override
 public void onAccessibilityEvent(AccessibilityEvent event) {

    try {
        AccessibilityNodeInfo rootNode = event.getSource();
        if (rootNode != null) {

            if (event.getEventType() == AccessibilityEvent.WINDOWS_CHANGE_REMOVED) {
                Log.d("tah", "event: " + event.getText().toString() );
                //accessibilityEvent.getText().toString()
                // will give all text which user typed in input box
            }


            int count = AccessibilityEvent.obtain().getRecordCount();
            Log.i("TAG", "count=" + count);
            for (int i = 0; i < count; i++) {
                // AccessibilityRecord の取得
                AccessibilityRecord record = AccessibilityEvent.obtain()
                        .getRecord(i);
                Log.i("TAG", "className=" + record.getClassName());
                // AccessibilityNodeInfo の取得
                AccessibilityNodeInfo node = record.getSource();
                if (node != null) {
                    Log.i("TAG", "parent=" + node.getParent().getClassName());
                    int childCount = node.getChildCount();
                    for (int j = 0; j < childCount; j++) {
                        Log.i("TAG", "child="
                                + node.getChild(j).getClassName());
                    }
                }
            }

        }

     } catch (Exception e) {
        Log.e("tag", "Exception: " + e.getMessage());
     }

 }

 @Override
 public void onInterrupt() {
    Log.e("tag", "onInterrupt: " );
 }

}

这就是问题所在。

转到应用信息,然后打开 on Autostart 也允许 no battery restrictions