AccessibilityService的performGlobalAction不起作用。

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

AccessibilityService performGlobalAction does not work

问题

我想使用AccessibilityService执行全局操作如返回截图等如果服务权限已禁用我会得到Nullpointerexception如果启用辅助功能服务权限则根本不会执行任何操作

MainActivity.java
```java
public class MainActivity extends AppCompatActivity {

    @RequiresApi(api = Build.VERSION_CODES.P)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button b = findViewById(R.id.b);
        ExecutionService service = new ExecutionService();
        b.setOnClickListener(v -> {
            service.performGlobalAction(AccessibilityService.GLOBAL_ACTION_TAKE_SCREENSHOT);
        });
    }
}

我的Accessibility Service

public class ExecutionService extends AccessibilityService {

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {

    }

    @Override
    public void onInterrupt() {

    }

}

辅助功能服务配置 xml

<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
    android:accessibilityEventTypes="typeNotificationStateChanged|typeWindowStateChanged|typeWindowContentChanged"
    android:accessibilityFeedbackType="feedbackGeneric"
    android:accessibilityFlags="flagDefault"
    android:canRetrieveWindowContent="true"
    android:description="@string/accessibility_description"
    android:notificationTimeout="100" />

清单声明

<service
    android:name=".ExecutionService"
    android:enabled="true"
    android:exported="true"
    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/auto_reply_service_config" />
</service>

我没有声明 <uses-permission android:name="android.permission.BIND_ACCESSIBILITY_SERVICE" /> 权限。


<details>
<summary>英文:</summary>

I want to use AccessibilityService to perform global actions such as back, take_screenshot and more.
if the service permission is disabled i get Nullpointerexception and if accessibility service permission is enabled it doesn&#39;t do anything at all.

MainActivity.java

public class MainActivity extends AppCompatActivity {

@RequiresApi(api = Build.VERSION_CODES.P)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button b = findViewById(R.id.b);
    ExecutionService service = new ExecutionService();
    b.setOnClickListener(v -&gt; {
        service.performGlobalAction(AccessibilityService.GLOBAL_ACTION_TAKE_SCREENSHOT);
    });
}

}

My Accessibility Service

public class ExecutionService extends AccessibilityService {

@Override
public void onCreate() {
    super.onCreate();
}

@Override
public void onAccessibilityEvent(AccessibilityEvent event) {

}

@Override
public void onInterrupt() {

}

}

accessibility service config xml

<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeNotificationStateChanged|typeWindowStateChanged|typeWindowContentChanged"
android:accessibilityFeedbackType="feedbackGeneric"
android:accessibilityFlags="flagDefault"
android:canRetrieveWindowContent="true"
android:description="@string/accessibility_description"
android:notificationTimeout="100" />

Manifest declaration

<service
android:name=".ExecutionService"
android:enabled="true"
android:exported="true"
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/auto_reply_service_config"/>
</service>

I did not declare the 

<uses-permission android:name="android.permission.BIND_ACCESSIBILITY_SERVICE" />

permission.





</details>


# 答案1
**得分**: 0

"ExecutionService service = new ExecutionService();" 不适用于辅助功能。

在启用或禁用智能设置后,该服务会手动启动或停止。

使用广播。

<details>
<summary>英文:</summary>

```ExecutionService service = new ExecutionService();```

Doesn&#39;t work for accessibility.

The service starts or stops after you enable or disable it manually in the smart settings.

Use broadcasts.

</details>



huangapple
  • 本文由 发表于 2023年5月6日 21:02:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76189048.html
匿名

发表评论

匿名网友

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

确定