github地址 :https://github.com/linhaosheng/WeChatHongBao_Master/tree/master
微信自动抢红包,目前只适配了6.3.32版本的,在android studio上导入,运行程序后点击开始检测,如果手机没有开启无障碍服务,则会调到设置页面进行打开无障碍服务,该app主要是用到了AccessibilityService 界面的ui控件的检测使用的是Google自带的uiautomatorviewer。
打开服务后监听三种状态 :
AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED 和 AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED 和
AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED 具体的代码如下:
switch (eventType) { //获取通知栏事件 case AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED: if (isScreenBlock) { wakeLock(); } List<CharSequence> text = event.getText(); if (!text.isEmpty()) { for (CharSequence sequence : text) { String message = String.valueOf(sequence); if (message.contains("微信红包")) { openNotify(event); } } } break; case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED: openHongBao(event); break; case AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED: findHongBao(event); break; }当通知栏显示有微信红包是 将触发:
if (event.getParcelableData() == null || !(event.getParcelableData() instanceof Notification)) { return; } Notification notification = (Notification) event.getParcelableData(); PendingIntent contentIntent = notification.contentIntent; try { contentIntent.send(); } catch (PendingIntent.CanceledException e) { e.printStackTrace(); }进行点击,进入聊天页面后,找到有红包的View进行点击:
System.out.println("evenClass-----" + event.getClassName()); if ("com.tencent.mm.plugin.luckymoney.ui.LuckyMoneyReceiveUI".equals(event.getClassName())) { // 拆红包界面 getPacket(this);; } else if ("com.tencent.mm.plugin.luckymoney.ui.LuckyMoneyDetailUI".equals(event.getClassName())) { // 拆完红包后,看红包金额的界面 getMoney(); } else if ("com.tencent.mm.ui.LauncherUI".equals(event.getClassName())) { // 聊天界面 openPacket(event); }
点击红包
private void openPacket(AccessibilityEvent event) { System.out.println("openPacket----"); AccessibilityNodeInfo rootInActiveWindow = getRootInActiveWindow(); if (rootInActiveWindow == null) { return; } List<AccessibilityNodeInfo> listPacket = rootInActiveWindow.findAccessibilityNodeInfosByText("领取红包"); if (listPacket != null) { for (int i = listPacket.size() - 1; i >= 0; i--) { AccessibilityNodeInfo parent = listPacket.get(i).getParent(); if (parent != null) { PerformClickUtils.performClick(parent); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } if (!"com.tencent.mm.plugin.luckymoney.ui.LuckyMoneyReceiveUI".equals(event.getClassName())) { PerformClickUtils.performBack(this); return; } } } }
拆红包 :
private void getPacket(AccessibilityService accessibilityService) { AccessibilityNodeInfo rootInActiveWindow = getRootInActiveWindow(); if (rootInActiveWindow == null) { return; } //若出现 “该红包已超过24小时。如已领取,可在“我的红包”中查看" 则直接返回 List<AccessibilityNodeInfo> back = rootInActiveWindow.findAccessibilityNodeInfosByViewId(hongbao_expire); if (back != null) { List<AccessibilityNodeInfo> nodeInfoId = rootInActiveWindow.findAccessibilityNodeInfosByViewId(hongbao_expire_close); //关闭ID if (nodeInfoId != null && !nodeInfoId.isEmpty()) { PerformClickUtils.findViewIdAndClick(this, hongbao_expire_close); } else { PerformClickUtils.performBack(this); } PerformClickUtils.findTextAndClick(this, "聊天信息"); } List<AccessibilityNodeInfo> hongbao = rootInActiveWindow.findAccessibilityNodeInfosByText("拆红包"); if (hongbao != null && !hongbao.isEmpty()) { PerformClickUtils.findTextAndClick(accessibilityService, "拆红包"); } else { List<AccessibilityNodeInfo> accessibilityNodeInfosByViewId = rootInActiveWindow.findAccessibilityNodeInfosByViewId(hongbao_open);//开红包的ID if (accessibilityNodeInfosByViewId != null && !accessibilityNodeInfosByViewId.isEmpty()) { PerformClickUtils.findViewIdAndClick(this, hongbao_open); } } }
获取红包的金额
private void getMoney() { //获取当前时间 date = new Date(System.currentTimeMillis()); String time = dateFormat.format(date); Money money = new Money(); money.setTime(time); System.out.println("time--" + time); AccessibilityNodeInfo rootInActiveWindow = getRootInActiveWindow(); if (rootInActiveWindow == null) { return; } //获取钱包来源 List<AccessibilityNodeInfo> accessibilityNodeInfosResource = rootInActiveWindow.findAccessibilityNodeInfosByViewId(hongbao_resource); if (accessibilityNodeInfosResource != null && accessibilityNodeInfosResource.size() > 0) { if (accessibilityNodeInfosResource.get(0) != null) { resource = accessibilityNodeInfosResource.get(0).getText().toString(); money.setResource(resource); } } //获取钱包金额 List<AccessibilityNodeInfo> accessibilityNodeInfosNumber = rootInActiveWindow.findAccessibilityNodeInfosByViewId(hongbao_number); if (accessibilityNodeInfosNumber != null && !accessibilityNodeInfosNumber.isEmpty()) { if (accessibilityNodeInfosNumber.get(0) != null) { number = accessibilityNodeInfosNumber.get(0).getText().toString(); money.setMoney(number); } } System.out.println("money----" + money.getMoney()); String objValue = Preferences.setObject(money); moneys.add(objValue); //点击返回按钮 List<AccessibilityNodeInfo> back = rootInActiveWindow.findAccessibilityNodeInfosByText("返回"); if (back != null) { PerformClickUtils.findTextAndClick(this, "返回"); } else { PerformClickUtils.performBack(this); } PerformClickUtils.findTextAndClick(this, "聊天信息"); }
详细代码在github上,欢迎start和fork