Accessibility Service - performGlobalAction() retu

2019-02-18 15:37发布

I am creating an Android Accessibility Service which calls performGlobalAction() at onStartCommand()

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;

}

I followed the guide and add necessary permissions to the manifest.

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

and

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

So my question is why the function call returns false. What is missing? And back button press event not happening by the way.

2条回答
够拽才男人
2楼-- · 2019-02-18 16:18

try Thread.sleep(1000) before performGlobalAction()

查看更多
叼着烟拽天下
3楼-- · 2019-02-18 16:33

In the phone's system settings -> accesivility service I had to enable my app. After that it started to work.

查看更多
登录 后发表回答