Implementation principle of Android wechat red envelope grabbing function

Approaching the lunar new year, there are more and more wechat red envelopes. There are many red envelope plug-in programs. I was curious about how to implement them, so I studied it and wrote an app for wechat to grab red envelopes in person. Now step by step to achieve it.

Realization idea

When the wechat red envelope grabbing program is started, it can identify and capture red envelopes at any time, and the service can realize the active function. When we start the service, the service keeps running in the background, constantly polling the messages in wechat, and immediately open the interface where the wechat red envelope is found. But how does he recognize the red envelope? You need to find the view of the node in the wechat red envelope. When you find the corresponding view, you can obtain the keyword or ID of the view, and automatically simulate clicking according to the keyword or ID to open the red envelope. Here is an accessibilityservice service. It is an auxiliary class that can realize these functions.

Accessibilityservice helper class

Accessibilityservice is an auxiliary class that can monitor the focus, window changes, button clicks, sound prompts, etc. of our mobile phone, but it must be opened manually, not through code. The auxiliary functions existing in the settings can be opened to enhance interaction. Now let's use it to grab red envelopes:

1. Create a new class qianghongbaoservice, which inherits from accessibilityservice

2. Registration manifest file

Create a new XML folder and create envelope_ service_ Config file

Logic processing of red envelope grabbing business

On the wechat red envelope grabbing interface, only two event types are concerned:

1、AccessibilityEvent.TYPE_ NOTIFICATION_ STATE_ Changed / / listen for messages in the notification bar

The message interface in the monitoring notification bar is mainly used to monitor every time the message sent by wechat is displayed in the notification bar, and get the message. When it is found that the obtained message contains "wechat red envelope", the interface displayed by the red envelope will be opened immediately.

2、AccessibilityEvent.TYPE_ WINDOW_ STATE_ Changed / / the current window changes. Do you want to enter the wechat chat interface

Here, you need to judge whether it is a chat interface according to the package name. If yes, click the red envelope. If not, judge whether it is a red envelope removal interface. If yes, open it and perform the red envelope removal action.

Package name of chat interface: com.tencent.mm.ui.launcherui

Package name of the red envelope removal interface: com.tencent.mm.plugin.luckymoney.ui.luckymoneyreceiveui

The red envelope disassembly in the above example is based on the ID of the control, or it can be performed according to the keyword "red envelope disassembly". However, the efficiency is slightly lower than that of removing red envelopes according to ID, which may affect the efficiency of grabbing red envelopes. It should be noted that the ID of removing red envelopes is different in different versions of wechat, so it needs to be changed frequently, which is troublesome. In addition, when the accessibilityservice service is closed, the oninterrupt method will be called back, and when connected, the onserviceconnected method will be called back. Here you can do some suggestive work.

Opening of red envelope grabbing function

The wechat red envelope grabbing function is opened manually. You can directly find the auxiliary functions in the settings and click open. For convenience, set a button for the activity to directly jump to the service function by means of implicit intention.

Here, the core functions of wechat red envelope grabbing are introduced. The efficiency of red envelope grabbing is also affected by the network, mobile phone memory size, system fluency, service priority and so on. Here is just a study, don't use it to often plug-in to grab red envelopes, be careful to be sealed!!! The source code has been uploaded to GitHub. Click to open the link if necessary

https://github.com/yoonerloop/WebEnvelope

The above is an analysis of the implementation principle of Android wechat red envelope grabbing function introduced by Xiaobian. I hope it will help you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

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
分享
二维码
< <上一篇
下一篇>>