Android实现QQ抢红包插件

时间:2022-09-25 07:33:22

又想到快要过年了,到时候还不知道群里要发好多红包,所以我将之前在网上宕的一份微信抢红包的代码修改了一下,实现了qq抢红包!可以支持抢qq拼手气红包,普通红包,口令红包,现在再也不怕20年单身手速的人跟我抢红包了!
先看测试效果图:
1.抢qq口令红包 

 Android实现QQ抢红包插件                           

 可以看见,只要红包一发出,自动填写口令并发出,帮你将红包抢到手!
2.抢qq拼手气红包                 

Android实现QQ抢红包插件

拼手气红包也是一样,只要红包一发出,自动帮你把红包抢到手,是不是很爽的感觉?
3.抢qq好友发送的红包

Android实现QQ抢红包插件

只要好友或者群里的人把红包一发出,就会第一时间让你抢到红包!所以只要在群里面开启插件,抢红包从来都是百发百中!好了废话不多说了,也不吹嘘有多牛多好了,下面直接给大家上代码:

mainactivity:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*mainactivity中的代码基本没改变:*/
public class mainactivity extends appcompatactivity {
  private final intent maccessibleintent = new intent(settings.action_accessibility_settings);
  private button switchplugin;
 
  @overrideprotected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate); 
    setcontentview(r.layout.activity_main); 
    switchplugin = (button) findviewbyid(r.id.button_accessible);
    updateservicestatus();
  }
 
  /*开启插件的按钮*/
  public void onbuttonclicked(view view) {startactivity(maccessibleintent);}
 
  @overrideprotected void onresume() {
    super.onresume();
    updateservicestatus();
  }
}
 
  private void updateservicestatus() { 
    boolean serviceenabled = false
    accessibilitymanager accessibilitymanager = (accessibilitymanager) getsystemservice(context.accessibility_service);
    list<accessibilityserviceinfo> accessibilityservices = accessibilitymanager.getenabledaccessibilityservicelist(accessibilityserviceinfo.feedback_generic); 
    for (accessibilityserviceinfo info : accessibilityservices) {  
      if (info.getid().equals(getpackagename() + "/.qqhongbaoservice")) {    
        serviceenabled = true;    
        break;   
       }
     }
     if (serviceenabled) { 
       switchplugin.settext("关闭插件");
       getwindow().addflags(windowmanager.layoutparams.flag_keep_screen_on);
     } else
       switchplugin.settext("开启插件"); 
       getwindow().clearflags(windowmanager.layoutparams.flag_keep_screen_on);}
   }
}

这里是mainactivity中的全部代码,是不是很少的样子,主要是实现了一个按钮去开启accessibility_service。这个插件主要就是借助accessibilityservice这个服务来实现。所以剩下的代码就都在这个服务中了!
qqhongbaoservice:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
public class qqhongbaoservice extends accessibilityservice {
 private static final string wechat_open_en = "open"
 private static final string wechat_opened_en = "you've opened"
 private final static string qq_default_click_open = "点击拆开";
 private final static string qq_hong_bao_password = "口令红包"
 private final static string qq_click_to_paste_password = "点击输入口令";
 private boolean mluckymoneyreceived; 
 private string lastfetchedhongbaoid = null
 private long lastfetchedtime = 0
 private static final int max_cache_tolerance = 5000
 private accessibilitynodeinfo rootnodeinfo; 
 private list<accessibilitynodeinfo> mreceivenode;
 
 @targetapi(build.version_codes.kitkat) 
 public void recycle(accessibilitynodeinfo info) {   
  if (info.getchildcount() == 0) {  
   /*这个if代码的作用是:匹配“点击输入口令的节点,并点击这个节点”*/ 
   if(info.gettext()!=null&&info.gettext().tostring().equals(qq_click_to_paste_password)) {
    info.getparent().performaction(accessibilitynodeinfo.action_click);     
    }    
   /*这个if代码的作用是:匹配文本编辑框后面的发送按钮,并点击发送口令*/
   if (info.getclassname().tostring().equals("android.widget.button") && info.gettext().tostring().equals("发送")) {       
    info.performaction(accessibilitynodeinfo.action_click);     
    }   
  } else {     
    for (int i = 0; i < info.getchildcount(); i++) {     
      if (info.getchild(i) != null) {          
       recycle(info.getchild(i));      
      }     
     }  
    
  }
 
 @targetapi(build.version_codes.jelly_bean) 
 @override 
 public void onaccessibilityevent(accessibilityevent event) {   
  this.rootnodeinfo = event.getsource();   
  if (rootnodeinfo == null) {     
   return;  
   }   
  mreceivenode = null;  
  checknodeinfo();   
  /* 如果已经接收到红包并且还没有戳开 */  
  if (mluckymoneyreceived && (mreceivenode != null)) {    
    int size = mreceivenode.size();    
    if (size > 0) {      
      string id = gethongbaotext(mreceivenode.get(size - 1));       
      long now = system.currenttimemillis();      
      if (this.shouldreturn(id, now - lastfetchedtime))          
       return;      
      lastfetchedhongbaoid = id;       
      lastfetchedtime = now;       
      accessibilitynodeinfo cellnode = mreceivenode.get(size - 1);        
      if (cellnode.gettext().tostring().equals("口令红包已拆开")) {         
       return;       
      }       
      cellnode.getparent().performaction(accessibilitynodeinfo.action_click);     
      if (cellnode.gettext().tostring().equals(qq_hong_bao_password)) {
         accessibilitynodeinfo rownode = getrootinactivewindow();        
      if (rownode == null) {          
       log.e(tag, "noteinfo is null");          
       return;         
      } else {         
         recycle(rownode);        
      }       
     }
     mluckymoneyreceived = false;     
     }  
    }
  }
 
 private void checknodeinfo() { 
  if (rootnodeinfo == null) {  
    return; 
  
  /* 聊天会话窗口,遍历节点匹配“点击拆开”,“口令红包”,“点击输入口令” */
  list<accessibilitynodeinfo> nodes1 = this.findaccessibilitynodeinfosbytexts(this.rootnodeinfo, new string[]{qq_default_click_open, qq_hong_bao_password, qq_click_to_paste_password, "发送"}); 
  if (!nodes1.isempty()) {  
    string nodeid = integer.tohexstring(system.identityhashcode(this.rootnodeinfo));   
    if (!nodeid.equals(lastfetchedhongbaoid)) { 
    mluckymoneyreceived = true;    
    mreceivenode = nodes1;  
     }    return; 
   }
  }
 
 private string gethongbaotext(accessibilitynodeinfo node) {
   /* 获取红包上的文本 */
   string content;
   try {   
     accessibilitynodeinfo i = node.getparent().getchild(0);   
     content = i.gettext().tostring();
     } catch (nullpointerexception npe) {
         return null
       
     return content;
  }
 
  private boolean shouldreturn(string id, long duration) {
    // id为空 
   if (id == null) return true
   // 名称和缓存不一致
   if (duration < max_cache_tolerance && id.equals(lastfetchedhongbaoid)) {  
     return true;
    }
   return false;
  }
 
  private list<accessibilitynodeinfo> findaccessibilitynodeinfosbytexts(accessibilitynodeinfo nodeinfo, string[] texts) {
    for (string text : texts) { 
      if (text == null) continue;  
      list<accessibilitynodeinfo> nodes = nodeinfo.findaccessibilitynodeinfosbytext(text); 
      if (!nodes.isempty()) {   
        if (text.equals(wechat_open_en) && !nodeinfo.findaccessibilitynodeinfosbytext(wechat_opened_en).isempty()) {      
          continue;   
        }     
        return nodes;  
      }
     
    return new arraylist<>();
  }
 
  @override
  public void oninterrupt() {}
}

qqhongbaoservice的全部代码也在这里,代码不多。首先,在这个服务中主要是通过findaccessibilitynodeinfosbytext这个方法去获我们需要的节点;然后用performaction(accessibilitynodeinfo.action_click)这个方法去点击红包节点,关键思路大概就是这样!另外如果是口令红包,我们需要先按照上面的步骤将红包戳开,然后通过performaction(accessibilitynodeinfo.action_click)去点击输入口令,最后再通过点击去发送即可实现!qqhongbaoservice需要在androidmanifest.xml文件中注册,
注册的<application>节点如下图:

Android实现QQ抢红包插件

总体来看,只是将微信抢红包的代码做了少量的修改,在这里要感谢各位大神对微信抢红包源码的贡献!最后也希望这篇文章能给大家有所帮助,在抢红包大战中虐死单身狗,再也不怕你20年的单身手速了!!!