Android achieved through micro-channel grab a red envelope accessibility principle briefly

Jane book article: https: //www.jianshu.com/p/e1099a94b979

Grab a red envelope attached open-source projects address code has been changed to Kotlin a whole, has been adapted to the latest micro-channel version 7.0.5, if you help it reward a star: https://github.com/xbdcc/GrabRedEnvelope

Foreword

I believe many people have seen the group is always someone to grab a red envelope quickly, also may have been used to grab a red envelope plug-in, then what principle they achieve it?

  • The general used the most is accomplished by clicking AccessibilityService monitoring services accessible UI simulation, this method does not require the phone Root.

  • Another way can be called directly by the micro-letter codes Xposed Hook. Hook required by phone or install Xposed Root virtual environment, and to decompile combination of source code analysis, find the key Hook points.

Today it does not require Root brief introduction of micro-channel manner Android grab a red envelope, if you wrote some scripts Uiautomator Android or Appium should know that the implementation process is very simple. Note: The following is based on Android version 7.0.3 version micro letter.

Knowledge Point

Benpian knowledge tools will be used, if Google do not understand what you know:
DDMS, AccessibilityService, Kotlin language

UI analysis

  • First we open the SDK installation directory, under the MAC can press Shift + Command + G shortcut keys to quickly enter Finlder.
    As my SDK directory is / Users / caochang / Library / Android / sdk. Open the folder into the tool monitor analysis tools. If it is, then it can open Eclipse DDMS directly

  • Phone connected to the computer USB debugging, you can check on whether via adb devices command, if the connection is unsuccessful try adb kill-server and adb start-server.

  • After the phone is connected depends on the selected page view of the current process name, as shown below selected APP current test, click the icon in the middle of the circle will display the current top Activity view as follows:


     
    Monitor main interface
  • Phones to send a red envelope and look at the view below, we can find the id and text:


     
    Micro-channel group chat UI received a red envelope
  • Similarly we click on the red envelope, red envelopes into the bomb box, click on the demolition of the ID can be found in Figure:

 
Red bomb dismantling UI frame

Code

This example used to 'com.github.xbdcc: Cutils: 0.0.10' some of the tools, codes formatted with Code Style-Kotlin.

  • First, we write their own method inside onAccessibilityEvent inheritance AccessibilityService class implements the judgment is to inform current or interface or content changed:
        when (event.eventType) {
            AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED -> {
                LogUtils.d("通知改变:$event")
            }
            AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED -> {
                LogUtils.d("界面改变:$event") openRedEnvelope(event) } AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED -> { LogUtils.d("内容改变:$event") clickRedEnvelope() } } 
  • Find the Activity class name and id or text codes:
    private val WECHAT_PACKAGE = "com.tencent.mm"
    private val WECHAT_LUCKYMONEY_ACTIVITY =
        "$WECHAT_PACKAGE.plugin.luckymoney.ui.LuckyMoneyNotHookReceiveUI" //微信红包弹框 private val RED_ENVELOPE_FLAG_ID = "com.tencent.mm:id/aq7" //聊天页面区分红包id private val RED_ENVELOPE_ID = "com.tencent.mm:id/aou" //聊天页面红包点击框控件id private val RED_ENVELOPE_OPEN_ID = "com.tencent.mm:id/cyf" //抢红包页面点开控件id 
  • Click on the red envelope red envelope found Code:
    private fun clickRedEnvelope() { //如果没找到红包就不继续往下执行 if (!AccessibilityServiceUtils.isExistElementById( RED_ENVELOPE_FLAG_ID, rootInActiveWindow ) ) return //点击红包 AccessibilityServiceUtils.findAndClickOneById(RED_ENVELOPE_ID, rootInActiveWindow) } 
  • Red box appears shells open page red Code:
    private fun openRedEnvelope(event: AccessibilityEvent) { //如果当前页面不是微信红包弹出框则不继续往下执行 if (WECHAT_LUCKYMONEY_ACTIVITY != event.className) return AccessibilityServiceUtils.findAndClickOneById(RED_ENVELOPE_OPEN_ID, rootInActiveWindow) } 

Test results

 
Test Gif

Epilogue

These are the simple realization of micro-channel demo automatically grab a red envelope, you can also do some refinements notification listener listens page judgment grab a red envelope, password and click Filter delay to determine whether the red envelope has been robbed and so on.

Demo complete code addresses

https://github.com/xbdcc/GrabRedEnvelope/tree/2.0.1/demo

Grab a red envelope APP Download:

http://xbdcc.cn/GrabRedEnvelope/index.html

Guess you like

Origin www.cnblogs.com/xbdcc/p/11276432.html