Kee Accessibilityservice Use

Source: Internet
Author: User

Transferred from: http://www.jianshu.com/p/ba298b8d5a6e

First, the use of Accessibilityservice

First write a class to inherit Accessibilityservice

public class MyAccessibilityService extends AccessibilityService {    @Override    public void onAccessibilityEvent(AccessibilityEvent event) {        //主要操作都在这里    }    @Override    public void onInterrupt() {  }...}

Next, create a new XML file under the Res/xml folder (don't ask me where the XML folder came from (: Зゝ∠)), called Aaa.xml (the name is free to pull ~ ~).

<?xml version="1.0" encoding="utf-8"?><accessibility-servicexmlns:android="http://schemas.android.com/apk/res/android"    android:accessibilityEventTypes="typeWindowStateChanged|typeWindowContentChanged|typeNotificationStateChanged"    android:accessibilityFeedbackType="feedbackAllMask"    android:accessibilityFlags="flagDefault"    android:canRetrieveWindowContent="true"    android:description="@string/intro"    android:notificationTimeout="10"    android:packageNames="com.tencent.mm"/>

Next, explain the following properties:

    • Description: This is a brief explanation of this auxiliary function for you.
    • Packagenames: The package name of the event that this servie wants to receive, and if it wants to receive more than one, it can be separated by a few package names.
    • Accessibilityeventtypes: This is the set of actions that it can monitor, such as the status bar to pull messages, interface changes, etc., can monitor one or more (by separating ' | ' ) Action, it has typeviewtextchanged,typenotificationstatechanged,typewindowcontentchanged,typeallmask and so on, other types can refer to here.
    • Accessibilityfeedbacktype: Provide feedback type, voice vibration and so on. It is also possible to set one or more (by separating ' | ') ) value, you can set Feedbackspoken, Feedbackallmask, and so on.
    • Notificationtimeout: The minimum period between two accessibility events of the same type. is to monitor the interval between an action and a monitor to the next action.
    • Canretrievewindowcontent: If you want to be able to retrieve the contents of the active window, this setting cannot be changed at run time.
    • Settingsactivity: Allows the user to modify the component name that sets the service.

After the Aaa.xml file has been written, you are ready to configure the Androidmanifest.xml file.

<service        android:name=".MyAccessibilityService"        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/aaa"/>   </service>

Remember, you must also add permissions.

<uses-permission android:name="android.permission.BIND_ACCESSIBILITY_SERVICE" />

OK, now that everything is ready and running the program, you can find your own function in the phone's settings-accessibility function.

Second, the function of stealing red envelopes

First of all to understand the Servie response to the event type, TYPE_NOTIFICATION_STATE_CHANGED is the notification bar event, TYPE_WINDOW_STATE_CHANGED is the window state changes, TYPE_WINDOW_CONTENT_CHANGED is the window content changes, these 3 is the most important event type, through AccessibilityEvent can get this event type, we need to make a judgment.

@Overridepublic void onAccessibilityEvent(AccessibilityEvent event) {    int eventType = event.getEventType();    switch (eventType) {        case AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED:            //TODO处理通知栏来的事件类型            break;        case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED:            //TODO        case AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED:            //TODO窗口出现变化的时候处理            break;    }}

Next, we'll deal with the notification bar


The message received to the status bar. png

You can see its event type and the key word "[red envelope]" for this time, so when handling the notification bar event, first determine if the text of the message has a keyword, and if so, open the interface.

private void getNotificationInfo(AccessibilityEvent event) {    Parcelable parcelable = event.getParcelableData();    if (parcelable instanceof Notification) {        Notification notification = (Notification) parcelable;        try {            notification.contentIntent.send();        } catch (PendingIntent.CanceledException e) {            e.printStackTrace();        }    }}

Opening the interface also means that the window has changed, so that onAccessibilityEvent new events are received. Next in the other two events to find red envelopes and open the operation of red envelopes.


Open red envelopes. png
Red envelope Details ". png

From the above two pictures can know, click to grab red Envelopes and click Open when their event type are TYPE_WINDOW_STATE_CHANGED , and focus on their class, so you can do in the TYPE_WINDOW_STATE_CHANGED inside event.getClassName(); and then based on this class to determine whether it is to do Rob red envelopes or open red envelopes.

case AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED:            rootNodeInfo = getRootInActiveWindow();//相当于获得了这个界面,可以在这上面找控件啊什么的            if (rootNodeInfo == null) return;            if (className.equals(com.tencent.mm.ui.LauncherUI)) {                getLuckyMoney();//找红包,找到红包就打开            } else if (className.equals(com.tencent.mm.plugin.luckymoney.ui.LuckyMoneyReceiveUI)) {                openLuckyMoney();//开红包                backLuckyMoney(eventType);//红包详情,红包抢光了,红包超时了做一个取消的动作            } else if (className.equals(com.tencent.mm.plugin.luckymoney.ui.LuckyMoneyDetailUI)) {                backLuckyMoney(eventType);            }            break;

The interface layout of red envelopes. png

Find red envelopes in rootnodeinfo inside findaccessibilitynodeinfosbytext ("Collect red envelopes") , if any, then return the red envelope Accessibilitynodeinfo . For the picture inside of TextView: Collect red envelopes, in order to anyway in Chat screen constantly find this open red envelopes, and then open red envelopes, close, open, close the dead loop, in looking for red envelopes here need to add a judgment, if this red envelope has been opened once, in the window does not change the time will not click Red Envelopes. The condition of judging is the TextView in the picture: Time and imageview:xxx Avatar.

public boolean Judgenode (Accessibilitynodeinfo node) {try {if (node = = null) return false;        Accessibilitynodeinfo Node1 = Node.getparent (). GetParent ();        int count = Node1.getchildcount ();        Result.setlength (0);            for (int i = 0; i < count; i++) {Accessibilitynodeinfo Node2 = Node1.getchild (i); if ("Android.widget.ImageView". Equals (Node2.getclassname ())) {Charsequence contentdescription = node2.getc                Ontentdescription ();            if (contentdescription! = null) result.append (contentdescription.tostring ()); } if ("Android.widget.TextView". Equals (Node2.getclassname ())) {Charsequence Thisnodetext = node                2.getText ();            if (thisnodetext! = null) result.append (thisnodetext.tostring ());        }} if (Result.equals (info)) return false;        info = hongbaoinfo;    return true; } catch (Exception e) {e.printstacktrAce ();    return false; }}

The red envelope found, don't forget to add to the end after the judgment node.performAction(AccessibilityNodeInfo.ACTION_CLICK) to open the Red envelope interface. Below is open red envelopes, need to find the middle of the red envelope "open" word, there are two ways to get this word, the first is the recursive search interface above the button, it is the interface of the only button, the second is the direct use of the button Resource-id, But it is said that this ID Teng x will often change it, not afraid of trouble can choose Oh.


The interface layout of stealing red envelopes. png

When you find it, don't forget to click on the node node.performAction(AccessibilityNodeInfo.ACTION_CLICK) .

After opening the red envelope details, this time to exit the interface back to chat interface, there is a red envelope is robbed, also need to return to chat interface, this time to Judge rootNodeInfo.findAccessibilityNodeInfosByText(text) , if found, then true, do performGlobalAction(GLOBAL_ACTION_BACK); it.

Detailed operation of all records completed, the current code also some blue chicken, but also need to change, first of all.

Resources:

Auto-grab red envelopes, automatic installation principle of Accessibilityservice
Do you really understand accessibilityservice?

Kee Accessibilityservice Use (Turn)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.