Accessibilityservice enables wechat to send red packets

In accessibility service, we can do simulation operations. The following describes the function of sending red packets through wechat through accessibility service

1. To configure accessibilityservice, you need to integrate accessibilityservice to perform operations.

public class TestAccessibilityService extends AccessibilityService {
  @Override
  public void onAccessibilityEvent(AccessibilityEvent event) {
    ...
  }

  @Override
  public void onInterrupt() {
    VirtualControlManager.getInstance().onCancel();
  }
}

When using, it needs to be configured in androidmanifest.xml

<service android:name=".TestAccessibilityService"
   android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"
   android:label="@string/accessibility_service_label">
  <intent-filter>
      <action android:name="android.accessibilityservice.AccessibilityService" />
  </intent-filter>

   <Meta-data
      android:name="android.accessibilityservice"
      android:resource="@xml/accessibility_service_config" />

</service>

Create an XML folder under the res folder to store accessibility_ service_ config.xml

<?xml version="1.0" encoding="utf-8"?>
<accessibility-service
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:accessibilityEventTypes="typeAllMask"
  android:accessibilityFeedbackType="FeedbackGeneric"
  android:accessibilityFlags="flagDefault|flagRetrieveInteractiveWindows|flagIncludeNotImportantViews"
  android:canRetrieveWindowContent="true"
  android:description="@string/accessibility_service_description"
  android:notificationTimeout="100"/>
  <!--accessibilityEventTypes:表示该服务对界面中的哪些变化感兴趣,即哪些事件通知,比如窗口打开,滑动,焦点变化,长按等.具体的值可以在AccessibilityEvent类中查到,如typeAllMask表示接受所有的事件通知.-->
  <!--accessibilityFeedbackType:表示反馈方式,比如是语音播放,还是震动-->
  <!--canRetrieveWindowContent:表示该服务能否访问活动窗口中的内容.也就是如果你希望在服务中获取窗体内容的化,则需要设置其值为true.-->
  <!--notificationTimeout:接受事件的时间间隔,通常将其设置为100即可.-->
  <!--packageNames:表示对该服务是用来监听哪个包的产生的事件-->

This sentence is very important and will be used later. Remember

android:accessibilityFlags="flagDefault|flagRetrieveInteractiveWindows|flagIncludeNotImportantViews"

2. Start wechat

Intent intent = new Intent(Intent.ACTION_MAIN);
//  intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent
//  .FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

        //加这个flag为了让微信能够进入的时候在会话列表
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        ComponentName cn = new ComponentName(“com.tencent.mm”,“com.tencent.mm.ui.LauncherUI”);
        intent.setComponent(cn);
        context.startActivity(intent);

The parameters in setflags of many wechat startup codes on the Internet are the ones I annotated, which can indeed start wechat, but it can not be guaranteed that after wechat startup, it will be the original message list, but any wechat interface (the interface where the user last operated wechat). In order to simulate the click operation, a lot of judgment interface codes and operations should be added, Then, can I start wechat in the message list? The answer is yes, that is, using the following flag will definitely go to the message list when starting wechat, eliminating our operation.

Intent.FLAG_ACTIVITY_CLEAR_TOP

3. Perform the operation

Through the onaccessibilityevent (...) method of accessibilityservice, we can obtain the information of the interface view, use getrootinactivewindow() to simulate the operation step by step through the ID of the view, and send the red envelope interface.

It should be noted here that when wechat jumps to the red envelope interface, there is a dialog to load animation. When many people jump to the red envelope interface, the object obtained through getrootinactivewindow() is either null or the node information is wrong. This is because a dialog is loaded quickly, and some machines can see the flash effect. If the mobile phone has good performance, The dialog may not be visible, but the node information cannot be obtained during code execution.

An attempt has been made to add a delay, but after the delay, the onaccessibilityevent method does not execute, or it is equal to No.

At this time, we need to use the code mentioned at the beginning:

android:accessibilityFlags="flagDefault|flagRetrieveInteractiveWindows|flagIncludeNotImportantViews"

After adding this code, we can get the view of the red envelope sending interface through onaccessibilityevent, and send a code to fill in the amount in the red envelope sending interface:

private void sendLuckMoney() {
    AccessibilityNodeInfo rootInfo = accessibilityService.getRootInActiveWindow();
    if (rootInfo != null) {
      List<AccessibilityNodeInfo> nodeInfoList = rootInfo.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/clu");
      for (AccessibilityNodeInfo nodeInfo : nodeInfoList) {
        Bundle arguments = new Bundle();
        arguments.putCharSequence(AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE,"10");
        nodeInfo.performAction(AccessibilityNodeInfo.ACTION_SET_TEXT,arguments);
      }

    }
  }

The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.

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