英文:
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'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 -> {
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't work for accessibility.
The service starts or stops after you enable or disable it manually in the smart settings.
Use broadcasts.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论