简体   繁体   English

如何从另一个服务调用AccessibilityService方法?

[英]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: 我试图调用一个存在于扩展一个类的方法AccessibilityService从延伸到另一个类Service是这样的:

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. 但是什么也没有发生,如果在MyAccessibilityAccessibilityService类)中调用了它,一切都可以正常工作。

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? 然后,如何从另一个服务调用MyAccessibility任何方法?


EDIT: 编辑:

I also tried change doAction() to be static , but performGlobalAction cannot be. 我也尝试将doAction()更改为static ,但是performGlobalAction不能为static

The solution is access MyAccessibility by a singleton: 解决方案是通过单例访问MyAccessibility

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(); 并调用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 . AccessibilityServices与普通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. 为了获得这些提升的特权,它们必须在自己的特殊Context运行,而获得该状态的唯一方法是从AccessibilityServices设置菜单中启动服务... TalkBack在哪里。

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); Intent intent = new Intent(this,HelloService.class); startService(intent); startService(意向);

https://developer.android.com/guide/components/services 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. 如果我们要专门调用此方法,则将此方法复制到常规类中并调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM