7

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.

laplasz
  • 3,359
  • 1
  • 29
  • 40
  • 1
    Is your accessibility service actually enabled and running? Set a break point in `AccessibilityService.onServiceConnected()` to check. Also, you can remove the `` tag, the documentation is incorrect only the system can receive this permission. – alanv Mar 22 '15 at 23:50

2 Answers2

3

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

laplasz
  • 3,359
  • 1
  • 29
  • 40
  • 1
    For sorry in my case it still returns false, though I enabled it, any suggestions? How can I check if my service is connected? – Ashraf Alshahawy Aug 17 '16 at 17:13
  • @AshrafAlshahawy connect the device to the notebook with android tools, set in the developer options `USB debug mode`, use command `adb logcat` to investigate the logs. – Anton Samokat Nov 28 '22 at 11:43
1

try Thread.sleep(1000) before performGlobalAction()

void
  • 111
  • 2
  • 1
    For others stopping by, your suggestion is helpful on another case. When 2 `performGlobalAction (GLOBAL_ACTION_BACK)` are called sequentially, only one gets executed. So the trick is to sleep for a time between the 2 calls (500ms in my case) –  Jan 02 '18 at 01:48