Principle analysis and development idea of wechat red envelope grabbing plug-in in Android

1、 Foreword

Since last year, wechat added the function of grabbing red envelopes, wechat's e-commerce trip has officially started and become popular. However, as Android developers, we are aware of many problems while grabbing red envelopes, that is, the speed of manually grabbing red envelopes is slow. Of course, there are many reasons. Maybe it's the network, and this is also the biggest reason. But other factors that can not be ignored should also be taken into account. For example, when the mobile phone is charged and locked, we don't know that someone has started sending red envelopes, so this is also the reason why we lost a large number of red envelopes. As for the network problem, our developers may not be able to solve it with relevant technologies (of course, in the view of Google and Facebook, their ideal is to be able to connect to the Internet anywhere, of course, in remote rural areas, but we expect them to be popular one day. Then it will be the real Internet). It's a little far away. Let's get back to the point. Today we'll look at using technology to solve other non network problems. When charging the lock screen, it can also automatically help us grab the red envelope. And you should know that the accuracy of machines in robbing red envelopes is 100%, which may be the difference between people and machines. If the accuracy rate is 100%, it depends on our efficient and accurate algorithm. Let's take a look at the principle and implementation.

Last year, when I saw how popular it was to grab red envelopes, as a developer, I was eager to develop a plug-in, but what we could think of at that time was to use:

adb shell monkey

Command to simulate clicking on the screen, but there is a problem in that way, that is, blind clicking without a clue, so there will be almost delays, and the click success rate is extremely low. So I didn't think of other methods at that time, because when I recently did work related to auxiliary functions, I found that this function can be used to grab red envelopes.

In fact, now we can search the major markets and see that there are many plug-ins to grab red envelopes. Of course, we are not used for commercialization. Here is just to analyze the principle. We will find that those plug-ins have a common feature: the first step is to guide users to turn on auxiliary functions.

2、 Principle analysis

As for accessibility service, if you don't know it, you can go to Google. This function is actually very useful, but its starting point is for people with physical disabilities, such as users with imperfect fingers. How can you slide the screen and open an application? The auxiliary function is to do these things. In fact, its function can be summarized in two sentences:

First, find the view node we want

Second, then simulate clicking to realize specific functions

We know that the view system in Android is a tree structure, so each view is a node. So we can find the specified node, so how can we find the node we want? Here, let's take a look at the usage of accessibility service

First, we need to integrate the accessibilityservice class

We need to customize a service and inherit accessibilityservice. Of course, we also need to declare this service in androidmanifest.xml:

Step 2: declare permissions and configure

This service needs to indicate a permission:

Of course, there is also a meta data declaration, which is the configuration of the accessibility service. Let's look at the contents of the configuration file:

Here we see that there are many options. Let's take a look at some common attributes:

1. Android: accessibilityeventtypes = "typeallmask" you can almost understand from the property name. This is used to set the type of response event. Of course, typeallmask responds to all types of events. Of course, there are click, long press, slide, etc.

2、android:accessibilityFeedbackType="FeedbackSpoken"

Set the way of feedback to users, including voice broadcasting and vibration. Some TTS engines can be configured to realize pronunciation.

3、android:notificationTimeout="100"

The setting of response time is needless to say

4、android:packageNames="com.example.android.apis"

You can specify the event to respond to an application. It is not required here because you want to respond to the events of all applications. The default is to respond to the events of all applications. For example, if we write an auxiliary program for wechat red envelope grabbing, we can fill in the wechat package name here and listen to the events generated by wechat.

be careful:

1. In addition to defining the configuration information in XML, we can also define it in code. We usually do it in the onserviceconnected () method

2. Here, we usually write the package name of the application we need to monitor here, but sometimes we need to monitor multiple applications. What should we do at this time?

At this time, we can do this:

First, we register the package names of multiple applications in the code, so that we can listen to multiple applications

The second method: we filter the package name in the onaccessibilityevent event event listening method (this method is the most commonly used)

Step 3: listen for the specified event in onaccessibilityevent method

For example, we need to listen for events with notification bar messages:

There are many types of events. We can view the source code of the accessibilityevent class:

There are many events here. We can see many of these events by name. We may all know. For example, when the window changes, when a view is clicked and scrolled, we can know. Then we have these events, we can do our things, because we know that the event is triggered.

Step 4: find the node we want to process

Here, the system provides two methods for us to find the desired node view

The first method is to search through the text content of the node view

Findaccessibilitynodeinfosbytext ("find content")

This method is used to find text content in views such as textview and button, which can be quickly found in this way.

The second is the ID name in the XML layout through the node view

findAccessibilityNodeInfosByViewId("@id/xxx")

This is generally difficult to know, but we can still do it when looking for system controls, because the ID of system controls can be known and unified. (for these two methods, we may know when writing a web crawler program that a node can be found in HTML through tag / name / ID and other information. The principle is similar)

Step 5: simulate clicking the specified event

We find the view node we want and call the method to simulate the event:

performAction(AccessibilityNodeInfo.ACTION_CLICK)

Call this method. Of course, the parameter here is to specify the name of the event. This corresponds to those events monitored in accessibilityevent one by one. Here is the simulated click event. Of course, we can simulate the scroll event and long press event of view.

3、 Actual case: wechat red envelope grabbing plug-in

Above, we have introduced the specific steps of auxiliary function development. Now, let's practice it through a simple example

Example: wechat automatic red envelope grabbing plug-in

First, let's take a look at the process of grabbing red envelopes on wechat:

Step 1: we will receive a wechat red envelope message in the notice bar

We monitor the events in the notification bar:

AccessibilityEvent.TYPE_ NOTIFICATION_ STATE_ CHANGED

Then check whether there is text content of [wechat red envelope] in the message in the notice bar

If so, go to step two

Step 2: we simulate opening the notification bar

Open wechat as shown below:

We find the node view containing the text content of receiving the red envelope, and then simulate clicking to enter the third step:

Step 3: we click to receive the red envelope

'as shown below:

Here, we are looking for the node view containing the text content of the red envelope, and then simulate clicking

Let's take a look at the specific implementation in the code:

There's nothing to say about the code. Just follow the three steps we mentioned earlier, but here we need to pay attention to the details:

1. When we listen to the message in the notification bar, we call the following code to click the message in the notification bar

2. After we simulate clicking on the notification bar message, we still need to listen: accessibilityevent.type_ WINDOW_ STATE_ The changed event, which we will often use in the future, is an event sent when the window changes. It is very common. For example, we can listen to topactivity through this event and get the package name. This is also a principle of implementing application lock.

3. When looking for and receiving a red envelope, we did a job when simulating clicking, that is, we found a clickable view from the view control of "receiving a red envelope" text, and then simulated clicking

Why do you do this here? In fact, the principle is very simple, because we don't know the interface layout of wechat or which view he setonclicklistener. We can write an example. The performaction method is only effective for the simulated click of the view that calls the setonclicklistener method. In fact, it can be seen from the source code of the view. I won't explain it here. So we need to find a view node from bottom to top until we find a view that can be clicked.

Technical extension:

In fact, we can also use dump view hierarchy for UI Automator in DDMS tool to analyze wechat UI structure. I found this method later, which is more effective than the above code, as shown in the following figure:

Here we can see the detailed layout of the view, the properties of each view, and the very important information resource ID, which is the ID defined in XML. For this ID, we can also use the findaccessibilitynodeinfosbyviewid ("@ ID / xxx") mentioned earlier to find the control. This is also a learning experience and learn to use DDMS to analyze the view structure.

4、 Extension

The principle analysis of wechat red envelope grabbing has been analyzed above, but there are still many problems to achieve the ultimate. For example, we still need to filter some red envelopes that have been received, so the efficiency is also very high. This is all about the accuracy of the algorithm. What I want to say here is that we can not only use auxiliary functions to grab red envelopes, but also realize many functions, such as

1. Silent installation

It may be difficult for us to obtain these two requirements, so now if we have auxiliary functions, we can do it:

We can monitor the installation interface of the system, get the installation node view, and then simulate clicking. Uninstallation is the same principle

2. Force stop application

We know that there are many methods to stop applications in Android, including kill process and stopservice, but some of these methods and applications have countermeasures. The method of forced stop we used before is to obtain root permission and call the system's forcestop API to stop, but the premise is still root. Now, if we have auxiliary functions, we can do this:

We can listen to the application details page of the system, find the node view that ends running, and then simulate clicking

Of course, I mentioned two simple examples above, and many auxiliary functions can be done. His advantage is that he does not need root permission. However, it also requires user authorization:

If the user is not authorized, then all the work can not be started, so this method is not omnipotent. Of course, a digression: with the auxiliary function, it is more dangerous than after root. For example, the red envelope grabbing plug-in above us. In fact, we can obtain the wechat address book information and the wechat payment password with a little modification. These things can be done, so we still need to think twice when authorizing as users.

5、 Summary

As for the auxiliary function, I didn't have much contact before. I used this function in a job, so I went to learn it. As my own interest, I extended the learning of how to write a wechat red envelope grabbing plug-in. At the same time, I can consider using the auxiliary function to do what we need to do before. Of course, the auxiliary function is a function developed by Google for people with physical disabilities. Our developers may use this function to expand the product. Of course, these are things that Google did not think of, but this is at least a way and way for our developers to solve problems in the future development path. Remember this function!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>