教你一步步实现Android微信自动抢红包

教你一步步实现Android微信自动抢红包,第1张

概述本文介绍微信自动抢红包的实现方法,主要实现以下几个功能:     1.自动拆开屏幕上出现的红包

本文介绍微信自动抢红包的实现方法,主要实现以下几个功能:

      1.自动拆开屏幕上出现的红包

      2.处于桌面或聊天列表时接收到红包信息时自动进入聊天界面并拆红包

      3.日志功能,记录抢红包的详细日志

实现原理

     1.利用AccessibilityService辅助服务,监测屏幕内容,实现自动拆红包的目的。

     2.利用ActiveAndroID数据库简单记录红包日志

     3.利用preference实现监控选项纪录

最终界面

抢红包核心代码

AccessibilityService配置

androID:accessibilityEventTypes 设置触发监听回调的事件类型;

androID:packagenames 设置监听的应用,这里监听的是微信,因此填上微信的包名com.tencent.mm

<accessibility-service xmlns:androID="http://schemas.androID.com/apk/res/androID"androID:accessibilityEventTypes="typeNotificationStateChanged|typewindowstateChanged|typeWindowContentChanged" androID:accessibilityFeedbackType="FeedbackGeneric" androID:accessibilityFlags="flagDefault" androID:canRetrIEveWindowContent="true" androID:description="@string/accessibility_description" androID:notificationTimeout="100" androID:packagenames="com.tencent.mm" androID:settingsActivity="com.oden.annotations.app.activity.ManActivity" />

在AndroIDManifest.xml中声明:

 <service   androID:name=".app.service.HongbaoService_"   androID:enabled="true"   androID:exported="true"   androID:label="@string/app_name"   androID:permission="androID.permission.BIND_ACCESSIBIliTY_SERVICE" >   <intent-filter>    <action androID:name="androID.accessibilityservice.AccessibilityService" />   </intent-filter>   <Meta-data    androID:name="androID.accessibilityservice"    androID:resource="@xml/accessibility_service_config" />  </service>

抢红包实现代码

接收系统发送来的AccessibilityEvent

 private static final String GET_RED_PACKET = "领取红包"; private static final String CHECK_RED_PACKET = "查看红包"; private static final String RED_PACKET_PICKED = "手慢了,红包派完了"; private static final String RED_PACKET_PICKED2 = "手气"; private static final String RED_PACKET_PICKED_DETAIL = "红包详情"; private static final String RED_PACKET_SAVE = "已存入零钱"; private static final String RED_PACKET_NOTIFICATION = "[微信红包]"; @OverrIDe public voID onAccessibilityEvent(AccessibilityEvent event) {  L.d("RECEIVE EVENT!");  if (watchedFlags == null) return;   /* 检测通知消息 */  if (!mMutex) {   if (watchedFlags.get("pref_watch_notification") && watchNotifications(event)) return;   if (watchedFlags.get("pref_watch_List") && watchList(event)) return;  }  if (!watchedFlags.get("pref_watch_chat")) return;  this.rootNodeInfo = event.getSource();  if (rootNodeInfo == null) return;  mReceiveNode = null;  mUnpackNode = null;  checkNodeInfo();   /* 如果已经接收到红包并且还没有戳开 */  if (mLuckyMoneyReceived && !mLuckyMoneyPicked && (mReceiveNode != null)) {   mMutex = true;   AccessibilityNodeInfo cellNode = mReceiveNode;   cellNode.getParent().performAction(AccessibilityNodeInfo.ACTION_CliCK);   mLuckyMoneyReceived = false;   mLuckyMoneyPicked = true;   L.d("正在打开!");  }   /* 如果戳开但还未领取 */  if (mNeedUnpack && (mUnpackNode != null)) {   AccessibilityNodeInfo cellNode = mUnpackNode;   cellNode.performAction(AccessibilityNodeInfo.ACTION_CliCK);   mNeedUnpack = false;   L.d("正在领取!");  }  if (mNeedBack) {   performGlobalAction(GLOBAL_ACTION_BACK);   mMutex = false;   mNeedBack = false;   L.d("正在返回!");   //总次数和金额统计   if (isGetMoney) {    T.showShort(this,"抢到一个红包: " + gotMoney + "元!");    totalMoney = totalMoney + gotMoney;    totalSuccessNum++;    myPrefs.totalMoney().put(totalMoney);    myPrefs.successNum().put(totalSuccessNum);    L.d("totalMoney: " + totalMoney);    L.d("totalSuccessNum: " + totalSuccessNum);    savetoLog(hongbaoInfo);    isGetMoney = false;   }  } }

检测监听事件的节点信息

private voID checkNodeInfo() {  L.d("checkNodeInfo!");  if (this.rootNodeInfo == null) return;   /* 聊天会话窗口,遍历节点匹配“领取红包”和"查看红包" */  List<AccessibilityNodeInfo> nodes1 = this.findAccessibilityNodeInfosByTexts(this.rootNodeInfo,new String[]{    GET_RED_PACKET,CHECK_RED_PACKET});  if (!nodes1.isEmpty()) {  L.d("!nodes1.isEmpty()");   AccessibilityNodeInfo targetNode = nodes1.get(nodes1.size() - 1);   if ("androID.Widget.linearLayout".equals(targetNode.getParent().getClassname()))//避免被文字干扰导致外挂失效   {    if (this.signature.generateSignature(targetNode)) {     mLuckyMoneyReceived = true;     mReceiveNode = targetNode;     L.d("signature:" + this.signature.toString());    }   } else {    L.d("this is text");   }   return;  }  List<AccessibilityNodeInfo> nodes2 = this.findAccessibilityNodeInfosByTexts(this.rootNodeInfo,new String[]{    "拆红包"});  if (!nodes2.isEmpty()) {   L.d("node2 != null");   for (AccessibilityNodeInfo nodeInfo : nodes2) {     if (nodeInfo.getClassname().equals("androID.Widget.button"))      nodeInfo.performAction(AccessibilityNodeInfo.ACTION_CliCK);   }  } else {    /* 戳开红包,红包还没抢完,遍历节点匹配“拆红包” */   AccessibilityNodeInfo node2 = (this.rootNodeInfo.getChildCount() > 3) ? this.rootNodeInfo.getChild(3) : null;   if (node2 != null && node2.getClassname().equals("androID.Widget.button")) {    mUnpackNode = node2;    mNeedUnpack = true;    isToGetMoney = true;    L.d("find red packet!");    return;   }  }   /* 戳开红包,红包已被抢完,遍历节点匹配“已存入零钱”和“手慢了” */  if (mLuckyMoneyPicked) {   List<AccessibilityNodeInfo> nodes3 = this.findAccessibilityNodeInfosByTexts(this.rootNodeInfo,new String[]{     RED_PACKET_PICKED,RED_PACKET_SAVE,RED_PACKET_PICKED2,RED_PACKET_PICKED_DETAIL});   if (!nodes3.isEmpty()) {    L.d("!nodes3.isEmpty()");        if (rootNodeInfo.getChildCount() > 1) {     L.d("RED_PACKET_PICKED!");    } else {     L.d("nodes3.get(0).toString(): " + nodes3.get(0).getText().toString());     if (!nodes3.get(0).getText().toString().equals(RED_PACKET_PICKED_DETAIL)) {      AccessibilityNodeInfo targetNode = nodes3.get(nodes3.size() - 1);      hongbaoInfo.getInfo(targetNode);      if (isToGetMoney) {       isGetMoney = true;       isToGetMoney = false;       gotMoney = hongbaoInfo.getMoney();       L.d("gotMoney: " + gotMoney);      }      L.d("RED_PACKET_SAVE!");      L.d("hongbaoInfo: " + hongbaoInfo.toString());     } else {      L.d("this packet is myself!");     }    }    mNeedBack = true;    mLuckyMoneyPicked = false;   }  } }

主要通过检测“领取红包”等关键文字信息来判断是否有新红包

检测收到红包时判断是否"androID.Widget.linearLayout",屏蔽聊天信息中的文字干扰

拆红包时,由于微信版本可能不同,同时进行两种判断,以兼容部分版本

拆完红包需自动返回,有以下几种情况:抢到了,手慢了,以及该红包是自己发出的红包

下面是监听聊天列表的代码:

private boolean watchList(AccessibilityEvent event) {  // Not a message  if (event.getEventType() != AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED || event.getSource() == null)   return false;  List<AccessibilityNodeInfo> nodes = event.getSource().findAccessibilityNodeInfosByText(RED_PACKET_NOTIFICATION);  if (!nodes.isEmpty()) {   AccessibilityNodeInfo nodetoClick = nodes.get(0);   CharSequence contentDescription = nodetoClick.getContentDescription();   if (contentDescription != null && !lastContentDescription.equals(contentDescription)) {    nodetoClick.performAction(AccessibilityNodeInfo.ACTION_CliCK);    lastContentDescription = contentDescription.toString();    return true;   }  }  return false; }

下面是监听通知信息的代码:

 private boolean watchNotifications(AccessibilityEvent event) {  // Not a notification  if (event.getEventType() != AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED)   return false;  // Not a hongbao  String tip = event.getText().toString();  if (!tip.contains(RED_PACKET_NOTIFICATION)) return true;  Parcelable parcelable = event.getParcelableData();  if (parcelable instanceof Notification) {   Notification notification = (Notification) parcelable;   try {    notification.contentIntent.send();   } catch (PendingIntent.CanceledException e) {    e.printstacktrace();   }  }  return true; }

红包信息的获取,及日志的存储

通过获取节点的子信息,分别获得红包发送者及抢到的金额、抢红包时间等信息,建立简单的表单分别记录该信息。

@table(name = "HongbaoInfos")public class HongbaoInfo extends Model { private int month; private int day; private int hour; private int min; private int sec; @Column(name = "sender") public String sender; @Column(name = "money") public String money; @Column(name = "time") public String time; public voID getInfo(AccessibilityNodeInfo node) {  AccessibilityNodeInfo hongbaoNode = node.getParent();  sender = hongbaoNode.getChild(0).getText().toString();  money = hongbaoNode.getChild(2).getText().toString();  time = getStringTime(); } private String getStringTime() {  Calendar c = Calendar.getInstance();  month = c.get(Calendar.MONTH) + 1;  day = c.get(Calendar.DAY_OF_MONTH);  hour = c.get(Calendar.HOUR_OF_DAY);  min = c.get(Calendar.MINUTE);  sec = c.get(Calendar.SECOND);  return month+"月"+day+"日 "+hour+":"+min+":"+sec; } @OverrIDe public String toString() {  return "HongbaoInfo [sender=" + sender + ",money=" + money + ",time=" + time + "]"; } public static List<HongbaoInfo> getAll() {  return new Select()    .from(HongbaoInfo.class)    .orderBy("ID ASC")    .execute(); } public static voID deletealL() {  new Delete().from(HongbaoInfo.class).execute(); } public float getMoney() {  return float.parsefloat(money); } public String getSender() {  return sender; } public String getTime() {  return time; }}

存储 *** 作:

 private voID savetoLog(HongbaoInfo hongbaoInfo) {  if (watchedFlags.get("pref_etc_log")) {   HongbaoInfo hongbaoInfo1 = new HongbaoInfo();   hongbaoInfo1 = hongbaoInfo;   hongbaoInfo1.save();  } else {   L.d("log closed!");  } }

总结

主要的代码到这里基本结束,目前在微信最新版上测试ok,尚还存在以下几个问题:

    1.同一个人连续发的不能自动抢,因为为了防止重复点击做了过滤,同一个人的红包抢了后不会再次点击

    2.AccessibilityService开启时间长后有时会被系统关掉

结束语

以上就是本文的全部内容了,希望对大家的学习和工作能有所帮助。

总结

以上是内存溢出为你收集整理的教你一步步实现Android微信自动抢红包全部内容,希望文章能够帮你解决教你一步步实现Android微信自动抢红包所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1141622.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存