绑定到Android中的AccessibilityService

绑定到Android中的AccessibilityService

本文介绍了绑定到Android中的AccessibilityService吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个Android Service ,该服务扩展了 AccessibilityService 。我想从我的活动绑定到该服务。因为我需要将信息从服务发送到活动。我需要示例代码。我搜索过google,但未找到任何类似内容,有人可以在示例中添加链接。

I have created an android Service, which extends an AccessibilityService. From my Activity, I would like to bind to that Service. Because I need to send information from Service to Activity. I need example code please. I have searched google and have not found anything similar, someone could put a link to an example please.

推荐答案

来自Activity的AccessibilityService是可能的,您只能将AccessibilityEvents传递到服务中。这是因为AccessibilityService.onBind()被声明为final,这意味着您将无法向活页夹中添加任何新方法。

While binding with an AccessibilityService from an Activity is possible, you'll only be able to pass AccessibilityEvents into the service. This is because AccessibilityService.onBind() is declared final, which means you won't be able to add any new methods to the binder.

但是,有几个替代方法。

There are, however, several alternative solutions.

如果您的活动和服务在同一流程中运行(如果它们在同一个APK中通常是正确的),则可以与静态无障碍服务的实例。这是一个如何工作的示例:

If your activity and service are running within the same process (this is typically true if they are in the same APK), then you can communicate with a static instance of your accessibility service. Here is an example of how that would work:

MyAccessibilityService.java:

private static MyAccessibilityService sSharedInstance;

protected void onServiceConnected() {
    . . .
    sSharedInstance = this;
}

public boolean onUnbind(Intent intent) {
    sSharedInstance = null;
    . . .
}

public static MyAccessibilityService getSharedInstance() {
    return sSharedInstance;
}

MyActivity.java

protected void onCreate() {
    . . .
    mAccessibilityService = MyAccessibilityService.getSharedInstance();
    if (mAccessibilityService != null) {
        // The service is running and connected.
        mAccessibilityService.doSomething();
    }
}

如果您的服务和活动处于单独的流程中(例如单独的APK),那么您仍然可以选择以下几种方式:

If your service and activity are in separate processes (e.g. separate APKs), then you still have a few options:


  • 通过广播从您的活动到

  • 将您的活动绑定到与静态实例通信的AccessibilityService流程中的代理服务(如上所述)

  • Communicate by broadcasting Intents from your activity to a BroadcastReceiver in your service
  • Bind your activity to a "proxy" service within the AccessibilityService's process that communicates with a static instance (as above)

如果您需要详细说明其中任何一项,请发表评论。

If you need elaboration on either of those, leave a comment.

这篇关于绑定到Android中的AccessibilityService吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 23:44