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.
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();
You can't. While the answers provided will cause the code to be run, it most certainly won't run in the way you're expecting it to. AccessibilityServices
are not like normal Services
. They get elevated priveleges. In order to get those elevated priveleges they must be run within their own special Context
, and the only way to get that state is to launch the service from the AccessibilityServices settings menu... Where TalkBack is.
YOU COULD... you shouldn't... but YOU COULD, start your service and use intra process communication mechanisms to get the function to run when you want it to from another process. This would be the only way to achieve what you want from a functional perspective.
I think we need to make an intent to call the service, then it'll be performing like a service.
Intent intent = new Intent(this, HelloService.class); startService(intent);
https://developer.android.com/guide/components/services
If we want to call this method specifically, then copy this method to a regular class and call it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.