Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How call a AccessibilityService method from another service?

I'm trying call a method present in a class that extends AccessibilityService from another class that extends Service this way:

MyAccessibility access = new MyAccessibility();
access.doAction(); // doAction(); is the method called

but nothing happens, already if this is called inside MyAccessibility (AccessibilityService class) all works fine.

public class MyAccessibility extends AccessibilityService {

@Override
    protected void onServiceConnected() {
        super.onServiceConnected();

        System.out.println("Accessibility was connected!");

        doAction();
    }

    public void doAction(){

    performGlobalAction(GLOBAL_ACTION_RECENTS);

    }

}

Then how i can call any method of MyAccessibility from a another service?


EDIT:

I also tried change doAction() to be static, but performGlobalAction cannot be.


1 Answers

The solution is access MyAccessibility by a singleton:

public class MyAccessibility extends AccessibilityService {

public static MyAccessibility instance;

@Override
    protected void onServiceConnected() {
        super.onServiceConnected();

        System.out.println("Accessibility was connected!");

        instance = this;

    }

    public void doAction(){

    performGlobalAction(GLOBAL_ACTION_RECENTS);

    }

}

And call doAction(); from your service like this:

MyAccessibility.instance.doAction();

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!