Use AccessibilityService to achieve automatic red packet snatching on WeChat and red packet snatching on WeChat

Source: Internet
Author: User

Use AccessibilityService to achieve automatic snatching of red packets and snatching of Red Packets

Recently, the AccessibilityService is used to implement the automatic red packet snatching function. Here we mainly write the logic and attention points.

Notes

1. Search for keywords

To implement a function, such as clicking, we need to find the corresponding object and simulate the click event. So the first step is how to find the object. Three methods are described below:

(1) findAccessibilityNodeInfosByText is searched by text, and List <AccessibilityNodeInfo> is returned. Therefore, you need to use the for loop to determine the object of the desired keyword.

(2) findAccessibilityNodeInfosByViewId is queried by the Control id. The returned List is <AccessibilityNodeInfo>. Although the List is returned, only one is returned, and the search accuracy is high, however, the system version API> = 18 is required.

(3) search with findAccessibilityNodeInfosByText. When you use uiautomatorviewer to view the layout, you can find that the same control IDs of different mobile phones are different. For example, when you need to query the number of red packets, you need to first find the 'meta', then obtain its parent control, and then find the location of the amount, which remains unchanged.

2. For the return Function

Generally, after receiving a red envelope, we enter the red envelope details page. In this case, we will return to the chat page and use uiautomatorviewer to view the return arrow and Its clickable = false. In this case, we will not be able to pass

AccessibilityNodeInfo. Condition maction (AccessibilityNodeInfo. ACTION_CLICK); To implement the return function by clicking events. However, check that the source code of AccessibilityService contains corresponding global events. The following describes two methods to implement the return function.
(1) Find the corresponding return button on the interface and use the AccessibilityNodeInfo
Encryption maction (AccessibilityNodeInfo. ACTION_CLICK) implements click. However, before performing the operation, determine whether isCheckable () is false.
(2) The following describes how to use the performGlobalAction of AccessibilityService:
/**     * Performs a global action. Such an action can be performed     * at any moment regardless of the current application or user     * location in that application. For example going back, going     * home, opening recents, etc.     *     * @param action The action to perform.     * @return Whether the action was successfully performed.     *     * @see #GLOBAL_ACTION_BACK     * @see #GLOBAL_ACTION_HOME     * @see #GLOBAL_ACTION_NOTIFICATIONS     * @see #GLOBAL_ACTION_RECENTS     */    public final boolean performGlobalAction(int action) {        IAccessibilityServiceConnection connection =            AccessibilityInteractionClient.getInstance().getConnection(mConnectionId);        if (connection != null) {            try {                return connection.performGlobalAction(action);            } catch (RemoteException re) {                Log.w(LOG_TAG, "Error while calling performGlobalAction", re);            }        }        return false;    }
Therefore, to implement the return function, you only need to call performGlobalAction (AccessibilityService. GLOBAL_ACTION_BACK); of course, if you want to implement Home and notification, replace the latest application with the corresponding action.
3. Interface classes
/*** Package name */static final String WECHAT_PACKAGENAME = "com. tencent. mm ";/*** red envelope removal class */static final String WECHAT_RECEIVER_CALSS =" com. tencent. mm. plugin. luckymoney. ui. luckyMoneyReceiveUI ";/*** red envelope details class */static final String WECHAT_DETAIL =" com. tencent. mm. plugin. luckymoney. ui. luckyMoneyDetailUI ";/*** main interface or chat interface */static final String WECHAT_LAUNCHER =" com. tencent. mm. ui. launcherUI ";
Here, we need to note that WECHAT_LAUNCHER should be used for the main interface and chat interface FragmentActivity + Fragment. As a result, if the user enters the main interface, the AccessibilityEvent will be called. TYPE_WINDOW_STATE_CHANGED. As a result, the AccessibilityEvent is no longer called when you enter the chat page again. TYPE_WINDOW_STATE_CHANGED, And the AccessibilityEvent will be called. TYPE_WINDOW_CONTENT_CHANGED, and AccessibilityEvent. TYPE_WINDOW_CONTENT_CHANGED will be called once the content changes, so AccessibilityEvent is generally used. TYPE_WINDOW_STATE_CHANGED is used as the monitoring event. Therefore, the judgment condition is added to solve this problem:
(1) trigger AccessibilityEvent. TYPE_WINDOW_STATE_CHANGED whether the event search list page contains the word "receive red packet". If not, set a variable
(2) If the AccessibilityEvent. TYPE_WINDOW_STATE_CHANGED is not triggered and the AccessibilityEvent. TYPE_WINDOW_CONTENT_CHANGED is triggered, the variables set earlier are determined based on the synthesis.
4. Add a red envelope to be renewed to avoid repeated amount
Although the red envelopes on the chat interface contain the words "receive red envelopes", they are actually split. The identification is whether there is "remove red envelopes ", if a red envelope is split, the amount in the corresponding details is calculated. 5. How to query all child controls cyclically
/*** @ Param info the current node * @ param matchFlag the text to be matched * @ param type operation type */public void recycle (AccessibilityNodeInfo info, String matchFlag, int type) {if (info! = Null) {if (info. getChildCount () = 0) {CharSequence desrc = info. getContentDescription (); switch (type) {case ENVELOPE_RETURN: // return if (desrc! = Null & matchFlag. equals (info. getContentDescription (). toString (). trim () {if (info. isCheckable () {info. encryption maction (AccessibilityNodeInfo. ACTION_CLICK);} else {Your mglobalaction (AccessibilityService. GLOBAL_ACTION_BACK) ;}} break ;}} else {int size = info. getChildCount (); for (int I = 0; I <size; I ++) {AccessibilityNodeInfo childInfo = info. getChild (I); if (childInfo! = Null) {Log. e (TAG, "index:" + I + "info" + childInfo. getClassName () + ":" + childInfo. getContentDescription () + ":" + info. getText (); recycle (childInfo, matchFlag, type );}}}}}
There are many source code for snatching red packets on the Internet. For other reasons, we will not publish them here. You can modify the source code on the Internet to implement the following functions:
(1) In the screenshot notification bar[Red envelopes]Text notification, and then jump to the red envelope Interface
(2) On the group chat page, all "receive red packets" on the current page will be automatically queried, and then the probability of adding red packets will be displayed cyclically.
(3) Accurately Save the amount and date of the red packet

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

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.