Android accessibilityservice implements wechat red envelope grabbing plug-in
In more settings or advanced settings of your mobile phone, we will find a barrier free function. Many people don't know what this function is. In fact, this function is to enhance the user interface to help people with disabilities, or people who may not be able to fully interact with the device for the time being
Its specific implementation is to run in the background through the accessibilityservice service and receive the callback of the specified event through the accessibilityevent. Such events indicate some state transitions in the user interface, such as: the focus has changed, a button has been clicked, and so on. Such a service can choose the ability to request the content of the active window. Simply put, accessibilityservice is a background monitoring service. When the content you monitor changes, it will call the callback method of the background service
Accessibilityservice usage
1.1 create service class
Write your own service class and override the onserviceconnected () method, onaccessibilityevent () method and oninterrupt () method
The following is an introduction to the methods commonly used in accessibility service
Disableself(): disables the current service, that is, when the service can stop running through this method. Findfoucs (int false): find the control with a specific focus type. Getrootinactivewindow(): if the configuration can obtain the window content, the root node of the current active window will be returned. Getseviceinfo(): obtain the configuration information onaccessibilityevent of the current service (accessibilityevent event): the callback function of accessibilityevent event. The system continuously sends accessibilityevent here through sendaccessibilityevent(). Performglobalaction (int action): perform global operations, such as returning, returning to the home page, opening the latest operation setserviceinfo (accessibilityserviceinfo) : set the configuration information of the current service getsystemservice (string name): get the system service onkeyevent (keyevent event): if the service is allowed to listen to key operations, this method is a callback of key events. Note that this process occurs onserviceconnected() before the system processes key events : triggered when the system successfully binds the service, that is, when you open the corresponding service in the settings, it will be triggered when the system successfully binds the service. Usually, we can do some initialization operations here. Oninterrupt(): callback when the service is interrupted
1.2 declaration of service
Since it is a background service, we need to configure the service information in manifests
We must note that any information configuration error will make the service unresponsive
Android: Label: displays the name of the service in the accessibility list
Android: Permission: bind needs to be specified_ ACCESSIBILITY_ Service permission, which is the intent filter required by systems above 4.0: this name is fixed
1.3 configuring service parameters
Configuring service parameters refers to configuring the time used to accept specified types of events, listen to specified packages, retrieve window contents, and obtain event types. There are two ways to configure service parameters:
Method 1: after Android 4.0, you can specify XML file for configuration through meta data tag. Method 2: dynamically configure parameters through code
1.3.1 method I
Add the meta data tag to the original manifest to specify the XML file
Next is accessibility_ service_ Configuration of config file
The following is an introduction to XML parameters
Accessibility event types: indicates which changes in the interface the service is interested in, that is, which event notifications, such as window opening, sliding, focus change, long press, etc. The specific value can be found in the accessibilityevent class. For example, typeallmask indicates that all event notifications are accepted. Accessibilityfeedbacktype: indicates the feedback method, such as voice playback or canretrievewindowcontent: indicates whether the service can access the content in the active window. That is, if you want to obtain the form content in the service, you need to set its value to true description: the description of the barrier free function is shown in the figure below
Notificationtimeout: the time interval for accepting events, which is usually set to 100. Packagenames: indicates which package is used by the service to listen for the generated events. Here, take the package name of wechat as an example
1.3.2 method II
Configure the accessibilityserviceinfo information for our accessibilityservice through code. Here, we can extract it into a method for setting
The accessibilityserviceinfo class is involved here. The accessibilityserviceinfo class is used to configure accessibilityservice information. This class contains a large number of constant fields for configuration and XML attributes. The common ones are: accessibilityeventtypes, canrequestfilterkeyevents, packagenames, etc
1.4 start service
Here, we need to manually open this function in the accessibility function, otherwise we can't continue. We can open the list of accessibility functions of the system through the following code
1.5 handling event information
Because we have listened to the notification bar and interface of the event, when the notification bar or interface of the specified packagenames changes, we will call back our event through onaccessibilityevent, and then process the event
When wechat receives the notification, a push message will arrive in the status bar, which will be sent by type_ NOTIFICATION_ STATE_ Changed monitors and executes the contents. When we switch the wechat interface or use wechat, it will be type_ WINDOW_ STATE_ Changed monitors and executes the contents
Accessibilityevent method
Geteventtype(): event type getsource(): get the node information corresponding to the event source getclassname(): get the type of the class corresponding to the event source. For example, if the click event is generated by a button, then the complete class name of the button is obtained. Gettext(): get the text information of the event source. For example, the event is sent by textview, At this time, the text property of textview is obtained. If the event source is a tree structure, the collection of all values with text attribute in the tree is obtained. Isenabled(): whether the event source (corresponding interface control) is available. Getitemcount(): if the event source is a tree structure, the number of child nodes of the root node of the tree will be returned
1.6 obtaining node information
After the change of the interface window is obtained, the node of the control should be obtained at this time. The node of the whole window is essentially a tree structure. The node information can be obtained by the following operations
1.6.1 get window node (root node)
AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
1.6.2 get the specified child node (control node)
1.7 simulation node Click
After we have obtained the node information, we can simulate clicking, long pressing and other operations on the control node. The accessibilitynodeinfo class provides the performaction () method for us to perform the simulation operation. See the official document for the specific operations. Here are some common operations
Implementation of red envelope grabbing plug-in
2.1 principle analysis
1. After receiving the push message of wechat red envelope, judge whether the message prompt of "[wechat red envelope]" appears in the push message. If it appears, click to enter the chat interface. 2. Through traversing the window tree node, find the node with the word "receive red envelope", click to enter, that is, red envelope, and the red envelope grabbing interface pops up. 3. In the red envelope grabbing interface, Get the node with the "on" button through the ID, then open the red envelope. 4. On the red envelope details page, get the node with the return button through the ID, click and return to the wechat chat interface
2.2 precautions
1. Because the button ID of each version of wechat is different, we need to modify the button ID in our program to achieve the adaptation of the version. 2. When obtaining the control ID, pay attention to whether its layout can be clicked, otherwise obtaining the non clickable control will make the program unresponsive
2.3 get control ID
When our mobile phone is connected to the USB cable, select the device in the Android device monitor and open the dump view hierarchy for UI Automator tool, which can obtain the control information
Gets the on button ID and the return button ID
2.4 code implementation
Note: the latest version 6.3.30 control ID of wechat is used here. If it is of other versions, please adapt it yourself
When a red envelope is sent, the log prints information
11-21 13:53:06.275 2909-2909 / com.handsome.boke2 E / Demo: enter wechat 11-21 13:53:06.921 2909-2909 / com.handsome.boke2 E / Demo: click red envelope 11-21 13:53:07.883 2909-2909 / com.handsome.boke2 E / Demo: open red envelope 11-21 13:53:08.732 2909-2909 / com.handsome.boke2 E / Demo: exit red envelope
You may think of doing some software to steal information, such as getting QQ password, Alipay password and so on. Ha ha, all EditText sets inputType to password type, and can not get their input values.
2.5 source code download: http://xiazai.jb51.net/201611/yuanma/Androidwxpackage (jb51.net).rar
This article has been sorted into "Android wechat development tutorial summary", welcome to learn and read.
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.