• QQ
  • nahooten@sina.com
  • 常州市九洲新世界花苑15-2

Android

AccessibilityService的应用

原创内容,转载请注明原文网址:http://homeqin.cn/a/wenzhangboke/jishutiandi/Android/2019/0612/536.html

 

非常近应用AccessibilityService实现业务,感受照旧蛮故意思的,随手写一下这个类的用法,未来要是再有必要用到的时候也对照利便复习查阅。

 

AccessibilityService是用于开发无停滞功效运用的api类,赞助残障人士应用app。一样的,应用它可以赞助我们对用户体验进行晋升,比方手机助手中的一键安置,免除了我们屡次点击的繁难,它还能赞助我们实现少许看似外挂般的插件,好比抢红包插件。

 

若何应用AccessibilityService:

 

起首,建立子类秉承AccessibilityService,AccessibilityService是服无的一种,辣么我们必要在Mainifest注册,同时增加相对应的权限和过滤条件如下图。

 

<service

    android:name="packagename.YourAccessibilityService"

    android:enabled="true"

    android:exported="true"

    android:label="@string/app_name"

    android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">

    <intent-filter>

        <action android:name="android.accessibilityservice.AccessibilityService" />

    </intent-filter>

 

    <meta-data

        android:name="android.accessibilityservice"

        android:resource="@xml/rob_service_config" />

</service>

AccessibilityService可以在xml中配置它的辅助信息即

 

<meta-data

        android:name="android.accessibilityservice"

        android:resource="@xml/rob_service_config" />

我们必要在res文件夹中增加对应的xml文件res--xml--rob_service_config.xml,内容如下

 

<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"

    android:accessibilityEventTypes="typeAllMask"

    android:accessibilityFeedbackType="feedbackSpoken"

    android:description="@string/accessibility_service_description"

    android:accessibilityFlags="flagDefault"

    android:canRetrieveWindowContent="true"

    android:notificationTimeout="100"

    android:packageNames="package_name_1,package_name_2" />

eventTypes代表该服无关注的事件范例,比方:

 

typeNotificationStateChanged  关照栏状况改变

typeViewClicked               点击事件

typeWindowStateChanged        窗体状况变更

typeAllMask                   阻挡所有的事件

等等等,这里我们可以凭据我们的必要去选定阻挡范例。

 

packageNames即我们想要监听的运用包名,可以监听多个运用,包名之间以","隔开。

 

一样的我们可以在代码中配置这些信息:

 

 @Override

    public void onCreate() {

        super.onCreate();

//      AccessibilityServiceInfo info = new AccessibilityServiceInfo();

//      info.packageNames = installPackge; //监听过滤的包名

//      info.eventTypes = AccessibilityEvent.TYPES_ALL_MASK; //监听哪些举动

//      info.feedbackType = AccessibilityServiceInfo.FEEDBACK_SPOKEN; //反应

//      info.notificationTimeout = 100; //关照的光阴

//      setServiceInfo(info);

    }

做完这些以后,开启服无,翻开配置中的辅助选项,选定你的辅助服无并开启,你便享用阻挡其余app种种操纵的快感了~辣么,阻挡到的事件在甚么处所呢,AccessibilityService是一个空洞类,它的焦点要领即是他的空洞要领

 

public abstract void onAccessibilityEvent(AccessibilityEvent event);

这个event即是我们阻挡到的事件,这个要领是异步执行的(固然了,万一这事件被阻挡并处分了半天,人家的app还让不让人用了),这个光阴有view中的accessibilityDelegate工具发出。

 

好了,事件拿到了,就能对内部的内容大做文章了,AccessibilityEvent非常紧张的即是它的eventType了,

 

/**

 * Gets the event type.

 *

 * @return The event type.

 */

public int getEventType() {

    return mEventType;

}

真相我们必要晓得这是甚么范例光阴才好继续往下做嘛,而后即是

 

String className = event.getClassName().toString()

由于监听的是别人家的app,我们不晓得我们想要晓得的页面类名是甚么,以是想要在指定页面做指定工作那就必要这个页面的类名,有了这个要领,你还会不晓得别人家的活动页面的类名叫甚么了吗?

 

接下来即是getSource了获取事件的节点信息,又大概我们可以应用

 

/**

 * Gets the root node in the currently active window if this service

 * can retrieve window content. The active window is the one that the user

 * is currently touching or the window with input focus, if the user is not

 * touching any window.

 * <p>

 * <strong>Note:</strong> In order to access the root node your service has

 * to declare the capability to retrieve window content by setting the

 * {@link android.R.styleable#AccessibilityService_canRetrieveWindowContent}

 * property in its meta-data. For details refer to {@link #SERVICE_META_DATA}.

 * </p>

 *

 * @return The root node if this service can retrieve window content.

 */

public AccessibilityNodeInfo getRootInActiveWindow() {

    return AccessibilityInteractionClient.getInstance().getRootInActiveWindow(mConnectionId);

}

获取该事件触发时的活动窗口,从这个活动窗口拿到我们想要的信息,好比包含某文字的控件,大概执行想要做的辅助事件,好比点击、转动等。AccessibilityNodeIfo工具包含了树状父子节点信息,

 

public List<AccessibilityNodeInfo> findAccessibilityNodeInfosByViewId(String viewId){...}

public List<AccessibilityNodeInfo> findAccessibilityNodeInfosByText(String text){...}

这两个要领可以找到包含某文字大概某控件id称号的节点

 

public boolean performAction(int action) {...}

public boolean performAction(int action, Bundle arguments){...}

这两个要领可以使该节点执行某个动作好比

 

AccessibilityNodeInfoA.CTION_CLICK

别的对于片面action,可以佩戴分外的参数

 

Bundle bundle = new Bundle();

bundle.putInt(AccessibilityNodeInfo.ACTION_ARGUMENT_SELECTION_START_INT,0);

bundle.putInt(AccessibilityNodeInfo.ACTION_ARGUMENT_SELECTION_END_INT,1);

nodeInfo.performAction(AccessibilityNodeInfo.ACTION_SET_SELECTION,bundle);

别的AccessibilityService本身有一个要领

 

* @see #GLOBAL_ACTION_BACK

 * @see #GLOBAL_ACTION_HOME

 * @see #GLOBAL_ACTION_NOTIFICATIONS

 * @see #GLOBAL_ACTION_RECENTS

 */

public final boolean performGlobalAction(int action)

让我们去执行全局的动作,如回退,回笼home页等。

 

 

 

 

 

AccessibilityService的应用大要即是如许了,至于再细致的东西就一个个的去文档中找,并一个个应用它们吧。

 

 

 

别的,再记一下应用AccessibilityService过程当中用到的tool

 

一个是D:\Users\XXX\Android\sdk\build-tools\23.0.0目次下的aapt.exe

应用cmd切换到aapt目次执行aapt dump badging <file_path.apk>可以查看指定apk的包名等细致内容;

 

aapt应用小结   这里有十分细致的先容,对于这里来说,我们只必要晓得目标运用的包名即可;

 

另一个是D:\Users\XXX\Android\sdk\tools目次下的uiautomatorviewer.bat

 

翻开此文件持续手机,点击工具的截屏按钮,稍后你就能看到该页面的结构档次以及各控件的信息(有我AccessibilityService必要的resId,如许妈妈在不用忧虑我找不到想要的那个nodeInfo了)。

 


上篇:上一篇:说说8.0下 Android 通知(Notification)
下篇:下一篇:Android几种Service常驻内存的小思绪