美文网首页微信程序员Android开发
妙用AccessibilityService黑科技实现微信自动加

妙用AccessibilityService黑科技实现微信自动加

作者: coder_pig | 来源:发表于2018-04-09 19:07 被阅读180次

    引言

    在上上周的周六和周日,我发了两篇利用itchat实现微信机器人的文章(Python):

    通过把脚本挂到服务器上,自此告别手动挡,不用去手动转发小宇宙,
    不用手动加好友,然后把别人一个个拉到我的Py交易群里。正当我
    暗自窃喜的时候,微信并没有放过我这只小猫咪。

    image
    package com.coderpig.wechathelper;
    
    import android.accessibilityservice.AccessibilityService;
    import android.app.Notification;
    import android.app.PendingIntent;
    import android.os.Bundle;
    import android.os.Handler;
    import android.util.Log;
    import android.view.accessibility.AccessibilityEvent;
    import android.view.accessibility.AccessibilityNodeInfo;
    
    import java.util.List;
    
    /**
     * 描述:微信监控服务类
     *
     * @author CoderPig on 2018/04/04 13:46.
     */
    
    public class HelperService extends AccessibilityService {
    
        private static final String TAG = "HelperService";
        private Handler handler = new Handler();
        private String userName = "123";
    
        @Override
        public void onAccessibilityEvent(AccessibilityEvent event) {
            int eventType = event.getEventType();
            CharSequence classNameChr = event.getClassName();
            String className = classNameChr.toString();
            Log.d(TAG, event.toString());
            switch (eventType) {
                case AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED:
                    if (event.getParcelableData() != null && event.getParcelableData() instanceof Notification) {
                        Notification notification = (Notification) event.getParcelableData();
                        String content = notification.tickerText.toString();
                        if (content.contains("请求添加你为朋友")) {
                            PendingIntent pendingIntent = notification.contentIntent;
                            try {
                                pendingIntent.send();
                            } catch (PendingIntent.CanceledException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                    break;
                case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED:
                    switch (className) {
                        case "com.tencent.mm.plugin.subapp.ui.friend.FMessageConversationUI":
                            addFriend();
                            break;
                        case "com.tencent.mm.plugin.profile.ui.SayHiWithSnsPermissionUI":
                            verifyFriend();
                            break;
                        case "com.tencent.mm.plugin.profile.ui.ContactInfoUI":
                            performBackClick();
                            break;
                        case "com.tencent.mm.ui.LauncherUI":
                            if (!userName.equals("123")) {
                                openGroup();
                            }
                            break;
                        case "com.tencent.mm.ui.contact.ChatroomContactUI":
                            if (!userName.equals("123")) {
                                inviteGroup();
                            }
                            break;
                        case "com.tencent.mm.ui.chatting.ChattingUI":
                            if (!userName.equals("123")) {
                                openGroupSetting();
                            }
                            break;
                        case "com.tencent.mm.plugin.chatroom.ui.ChatroomInfoUI":
                            if (userName.equals("123")) {
                                performBackClick();
                            } else {
                                addToGroup();
                            }
                            break;
                        case "com.tencent.mm.ui.base.i":
                            dialogClick();
                            break;
                    }
                    break;
                case AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED:
    
            }
        }
    
        private void addFriend() {
            AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
            if (nodeInfo != null) {
                List<AccessibilityNodeInfo> list = nodeInfo
                        .findAccessibilityNodeInfosByText("接受");
                if (list != null && list.size() > 0) {
                    for (AccessibilityNodeInfo n : list) {
                        n.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                    }
                } else {
                    performBackClick();
                }
            }
        }
    
        private void verifyFriend() {
            AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
            //获得用户名
            if (nodeInfo != null) {
                userName = nodeInfo.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/d0n").get(0).getText().toString();
                AccessibilityNodeInfo finishNode = nodeInfo.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/hd").get(0);
                finishNode.performAction(AccessibilityNodeInfo.ACTION_CLICK);
            }
        }
    
        private void openGroup() {
            AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
            if (nodeInfo != null) {
                List<AccessibilityNodeInfo> nodes = nodeInfo.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/ca5");
                for (AccessibilityNodeInfo info : nodes) {
                    if (info.getText().toString().equals("通讯录")) {
                        info.getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
                        handler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
                                if (nodeInfo != null) {
                                    List<AccessibilityNodeInfo> nodes = nodeInfo.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/j5");
                                    for (AccessibilityNodeInfo info : nodes) {
                                        if (info.getText().toString().equals("群聊")) {
                                            info.getParent().getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
                                            break;
                                        }
                                    }
                                }
                            }
                        }, 500L);
                    }
                }
            }
        }
    
        private void inviteGroup() {
            AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
            if (nodeInfo != null) {
                List<AccessibilityNodeInfo> nodes = nodeInfo.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/a9v");
                for (AccessibilityNodeInfo info : nodes) {
                    if (info.getText().toString().equals("小猪的Python学习交流群")) {
                        info.getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
                        break;
                    }
                }
            }
        }
    
        private void openGroupSetting() {
            AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
            if (nodeInfo != null) {
                nodeInfo.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/he").get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK);
            }
        }
    
        private void addToGroup() {
            AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
            if (nodeInfo != null) {
                List<AccessibilityNodeInfo> listNodes = nodeInfo.findAccessibilityNodeInfosByViewId("android:id/list");
                if(listNodes != null && listNodes.size() > 0) {
                    AccessibilityNodeInfo listNode = listNodes.get(0);
                    listNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                    listNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                    final AccessibilityNodeInfo scrollNodeInfo = getRootInActiveWindow();
                    if (scrollNodeInfo != null) {
                        handler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                List<AccessibilityNodeInfo> nodes = scrollNodeInfo.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/d0b");
                                for (AccessibilityNodeInfo info : nodes) {
                                    if (info.getContentDescription().toString().equals("添加成员")) {
                                        info.getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
                                        break;
                                    }
                                }
                            }
                        },1000L);
                        handler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                List<AccessibilityNodeInfo> editNodes = getRootInActiveWindow().findAccessibilityNodeInfosByViewId("com.tencent.mm:id/arz");
                                if(editNodes != null && editNodes.size() > 0) {
                                    AccessibilityNodeInfo editNode = editNodes.get(0);
                                    Bundle arguments = new Bundle();
                                    arguments.putCharSequence(AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE, userName);
                                    editNode.performAction(AccessibilityNodeInfo.ACTION_SET_TEXT, arguments);
                                }
                            }
                        }, 2300L);
                        handler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                List<AccessibilityNodeInfo> cbNodes = getRootInActiveWindow().findAccessibilityNodeInfosByViewId("com.tencent.mm:id/kr");
                                if(cbNodes != null) {
                                    AccessibilityNodeInfo cbNode = null;
                                    if(cbNodes.size() == 1) {
                                        cbNode = cbNodes.get(0);
                                    } else if(cbNodes.size() == 2) {
                                        cbNode = cbNodes.get(1);
                                    }
                                    if (cbNode != null) {
                                        cbNode.getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
                                        AccessibilityNodeInfo sureNode = getRootInActiveWindow().findAccessibilityNodeInfosByViewId("com.tencent.mm:id/hd").get(0);
                                        sureNode.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                                    }
                                }
                            }
                        }, 3000L);
                    }
                }
    
            }
    
        }
    
        private void dialogClick() {
            AccessibilityNodeInfo inviteNode = getRootInActiveWindow().findAccessibilityNodeInfosByViewId("com.tencent.mm:id/aln").get(0);
            inviteNode.performAction(AccessibilityNodeInfo.ACTION_CLICK);
            userName = "123";
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    List<AccessibilityNodeInfo> sureNodes = getRootInActiveWindow().findAccessibilityNodeInfosByViewId("com.tencent.mm:id/aln");
                    if(sureNodes != null && sureNodes.size() > 0) {
                        AccessibilityNodeInfo sureNode = sureNodes.get(0);
                        sureNode.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                    }
    
                }
            },1000L);
        }
    
        private void performBackClick() {
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);
                }
            }, 300L);
        }
    
        //遍历控件的方法
        public void recycle(AccessibilityNodeInfo info) {
            if (info.getChildCount() == 0) {
                Log.i(TAG, "child widget----------------------------" + info.getClassName().toString());
                Log.i(TAG, "showDialog:" + info.canOpenPopup());
                Log.i(TAG, "Text:" + info.getText());
                Log.i(TAG, "windowId:" + info.getWindowId());
                Log.i(TAG, "desc:" + info.getContentDescription());
            } else {
                for (int i = 0; i < info.getChildCount(); i++) {
                    if (info.getChild(i) != null) {
                        recycle(info.getChild(i));
                    }
                }
            }
        }
    
        @Override
        public void onInterrupt() {
    
        }
    
    
    }
    
    

    来啊,Py交易啊

    想加群一起学习Py的可以加下,智障机器人小Pig,验证信息里包含:
    PythonpythonpyPy加群交易屁眼 中的一个关键词即可通过;

    image

    验证通过后回复 加群 即可获得加群链接(不要把机器人玩坏了!!!)~~~
    欢迎各种像我一样的Py初学者,Py大神加入,一起愉快地交流学♂习,van♂转py。

    image

    相关文章

      网友评论

      • 哼唱小情歌儿:您好 AccessibilityService 怎么让edittext 自动输入完成后 模拟点击回车键 比如我输入“这是自动发送的消息”+“\n” 这个对edittext 无效 并没有直接确认提交
      • ___简爱:很不错啊 希望楼主多多更新文章啊
      • ___简爱:很不错,学习一下
      • Robo_G:发现博主的文章都特别有意思!
      • Nuance__:要用到java。有没有粗暴点 用python的安卓自动化解决方案:smile:
        Nuance__:@coder_pig 嗯。我昨天发现了一个针对AS封装的js库叫autojs。脚本语言写脚本。
        coder_pig:appuim可以,不过每次运行程序微信都要重新登录,登录过于频繁的话会封一天

      本文标题:妙用AccessibilityService黑科技实现微信自动加

      本文链接:https://www.haomeiwen.com/subject/tcxwhftx.html