我正在创建一个 Android 无障碍服务,它在 onStartCommand()
中调用 performGlobalAction()。
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("service", "started");
Bundle extras = intent.getExtras();
Integer value = -1;
if (extras != null) {
value = extras.getInt("control");
}
switch(value) {
case BUTTON_EVENT_BACK:
//press back button
boolean result = performGlobalAction(GLOBAL_ACTION_BACK);
Log.d("make back action result: ", Boolean.toString(result));
break;
}
stopSelf();
return Service.START_STICKY;
}
我按照指南添加了必要的权限到清单中。
<service android:name=".MyAccessibilityService"
android:label="@string/app_name"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>
</service>
并且
<uses-permission android:name="android.permission.BIND_ACCESSIBILITY_SERVICE" />
所以我的问题是为什么函数调用返回false。缺少了什么?顺便说一下,返回按钮事件也没有发生。
AccessibilityService.onServiceConnected()
中设置断点进行检查。此外,您可以删除<uses-permission ... />
标签,因为文档是错误的,只有系统才能接收此权限。 - alanv