临近元旦,即将有一大波红包向我们袭来,有个轻松抢红包的工具岂不是更好,那就写个抢红包的应用吧,至于原理,网上有一大堆分享,我就不再详述了,只是大致将流程整理下,以作笔记.
一.界面
二 .功能实现原理
主要是用到了Accessibility,它是Android从API 4开始提供的一个功能,主要目的是帮助一些因为有视觉,听觉,身体障碍而无法完全使用触摸屏或铃声等的用户来使用Android的,但现在很多开发者都用它来实现一些其他功能,比如说微信抢红包,自动安装APK,强制停止应用等.实际上就是通过监视一个应用的界面元素状态的变化,来自动响应执行一些操作.
附API:http://www.android-doc.com/guide/topics/ui/accessibility/index.html
三. 主要功能实现
1.微信,QQ自动抢红包
2.微信,QQ聊天界面自动抢红包
3.随机回复感谢语
4.红包个数金额统计
四.功能实现流程
(1)在非微信消息列表界面,收到通知消息的事件,判断通知栏里的文本是否有[微信红包]的关键字——(2)进入微信,判断是否有[领取红包]的关键字—–(3)点击红包—–(4)点击领取按钮—–(5)收集红包数据—–(6)点击关闭红包详情按钮—–(7)自动回复随机感谢语
如果是在聊天界面,则从第(2)步开始,QQ领取红包大致相同,仅少了第四步.
五.部分代码分享
/*** 响应并处理AccessibilityEvent*/
switch (eventType) {case AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED:try {Thread.sleep(1000); // 停1秒} catch (InterruptedException e) {e.printStackTrace();}eventText = "TYPE_NOTIFICATION_STATE_CHANGED";List<CharSequence> texts = event.getText();if (!texts.isEmpty()) {for (CharSequence charSequence : texts) {String text = String.valueOf(charSequence);if (text.contains(WX_HONGBAO_TEXT_KEY)||text.contains(QQ_HONGBAO_TEXT_KEY)) {openNotify(event);break;}}}break;case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED:eventText = "TYPE_WINDOW_STATE_CHANGED";// 从微信主界面进入聊天界面openWeChatPage(event);break;case AccessibilityEvent.CONTENT_CHANGE_TYPE_SUBTREE:eventText = "CONTENT_CHANGE_TYPE_SUBTREE";// 从微信主界面进入聊天界面openWeChatPage(event);break;case AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED://聊天界面新消息eventText = "TYPE_WINDOW_CONTENT_CHANGED";if(StaticData.WXNserviceStatus&&StaticData.WXserviceStatus) {wxHongBaoservice.getRedPackets(QiangHongBaoService.this);}if(StaticData.QQNserviceStatus&&StaticData.QQserviceStatus) {qqHongBaoservice.getRedPackets(QiangHongBaoService.this);}break;}
/*** 聊天界面判断红包处理的逻辑*/private void openWeChatPage(AccessibilityEvent event) {CharSequence className = event.getClassName();Log.d(TAG, "openWeChatPage--className---" + className);new CheckScreenOn().checkScreen(getApplicationContext());if (wxSupportUi.getHbClick().equals(className)) {isWXNotify = true;wxHongBaoservice.clickOpenButton(QiangHongBaoService.this);} else if (wxSupportUi.getChartUi().equals(className) || wxSupportUi.getChartUi1().equals(className)) {wxHongBaoservice.getRedPackets(QiangHongBaoService.this);} else if (wxSupportUi.getMoneyDetails().equals(className)&&isWXNotify) {isWXNotify = false;wxHongBaoservice.getWXHBInfo(QiangHongBaoService.this,player,moneys,wxSupportUi);} else if (qqSupportUi.getChartUi().equals(className)) {isQQNotify = true;qqHongBaoservice.getRedPackets(QiangHongBaoService.this);}else if(qqSupportUi.getSendUi().equals(className)&&isQQNotify){//发红包界面判断isQQNotify = false;} else if(qqSupportUi.getMoneyDetails().equals(className)&&isQQNotify){isQQNotify = false;qqHongBaoservice.getQQHBInfo(QiangHongBaoService.this,player,moneys);}}
end