微信自动回复和自动抢红包实现原理(三):自动抢红包
閱讀該文章前,可以先閱讀我前兩篇文章
微信自動回復和自動搶紅包實現原理(一):AccessibilityService的介紹和配置
微信自動回復和自動搶紅包實現原理(二):自動回復
經過前兩篇文章的閱讀,我相信大家應該對AccessibilityService有一定的了解了,是不是已經按捺不住,想自己動手試試?先別急,可以再看完我這篇文章還不遲,相信你另有收獲的。接下來我們來探索一下自動搶紅包的實現原理。
看了我第二篇微信自動回復文章的朋友應該知道怎么做了,只是一些操作上不同:
好吧,我們給測試手機微信發個紅包,先打印log來看看,具體信息不貼了,直接看結果:
打開微信的界面 ------------------------------------------------------------- PackageName:com.tencent.mm Source Class:com.tencent.mm.ui.LauncherUI Description:null Event Type(int):32 ------------------------------------------------------------- 紅包接收界面(不管紅包還沒搶光還是已被搶光都會打開這個界面) ------------------------------------------------------------- PackageName:com.tencent.mm Source Class:com.tencent.mm.plugin.luckymoney.ui.LuckyMoneyReceiveUI Description:null Event Type(int):32 ------------------------------------------------------------- 紅包詳情界面(也就是搶到紅包以后的界面) ------------------------------------------------------------- PackageName:com.tencent.mm Source Class:com.tencent.mm.plugin.luckymoney.ui.LuckyMoneyDetailUI Description:null Event Type(int):32 -------------------------------------------------------------經我測試,LauncherUI只有微信在后臺才會觸發,但微信在前臺時也會有Notification,所以有LuckyMoneyReceiveUI、LuckyMoneyDetailUI兩個界面我們已經足夠了
接下來的工作是找到相應的控件了。先找紅包這個控件,給測試機微信發多幾次紅包,找到每個紅包的相同點。
紅包.png
看到紅色框框沒,“領取紅包”這個控件每次都不變了,可以根據其找到其父控件,再點擊就可以打開紅包了!
接下來就是紅包界面的“開”按鈕了。很遺憾,因為該按鈕即每文本信息,也沒特別的子控件,沒辦法,只能直接用控件的id了(但這種方法不好,因為據了解,控件的id經常會變,可能就會了防止這類插件的出現吧,哈哈),下面介紹如何獲取控件的id。
1.打開DDMS,連接手機,打開一個紅包,進入紅包界面,點擊下面按鈕
DDMS.png
2.選中你需要的控件,例如這里我們是要查看“開”按鈕這控件
開紅包.png
3.在右邊就可以查看控件的信息了,右下方可以查看id
QQ截圖20160809224615.png
嗯,對的,紅包的控件也可以這樣獲取,但我說過了,id是會變的,所以能不用就最好不要用。還有如果有朋友知道不用id獲取“開”按鈕的話,請告訴我一聲哈。
好了,所有難點都解決了,接下來只要寫代碼處理下邏輯就好了,直接上代碼吧。
*** 自動搶紅包服務*/ public class AutoOpenLuckyMoneyService extends AccessibilityService{private static final String TAG = AutoOpenLuckyMoneyService.class.getSimpleName();private static final int MSG_BACK_HOME = 0;private static final int MSG_BACK_ONCE = 1;boolean hasNotify = false;boolean hasLuckyMoney = true;public void onAccessibilityEvent(AccessibilityEvent event) {int eventType = event.getEventType(); // 事件類型switch (eventType) {case AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED: // 通知欄事件Log.i(TAG, "TYPE_NOTIFICATION_STATE_CHANGED");if(PhoneController.isLockScreen(this)) { // 鎖屏PhoneController.wakeAndUnlockScreen(this); // 喚醒點亮屏幕}openAppByNotification(event); // 打開微信hasNotify = true;break;default:Log.i(TAG, "DEFAULT");if(hasNotify) {AccessibilityNodeInfo rootNode = getRootInActiveWindow();clickLuckyMoney(rootNode); // 點擊紅包String className = event.getClassName().toString();if (className.equals(UI.LUCKY_MONEY_RECEIVE_UI)) { //紅包接收界面if(!openLuckyMoney()) { // 如果紅包被搶光了,就返回主界面backToHome();hasNotify = false;}hasLuckyMoney = true;} else if (className.equals(UI.LUCKY_MONEY_DETAIL_UI)) { // 搶到紅包backToHome();hasNotify = false;hasLuckyMoney = true;} else { // 處理沒紅包的情況,直接返回主界面if(!hasLuckyMoney) {handler.sendEmptyMessage(MSG_BACK_ONCE);hasLuckyMoney = true; // 防止后退多次}}}break;}}public void onInterrupt() {}/*** 打開微信* @param event 事件*/private void openAppByNotification(AccessibilityEvent event) {if (event.getParcelableData() != null && event.getParcelableData() instanceof Notification) {Notification notification = (Notification) event.getParcelableData();try {PendingIntent pendingIntent = notification.contentIntent;pendingIntent.send();} catch (PendingIntent.CanceledException e) {e.printStackTrace();}}}/*** 搜索并點擊紅包*/private void clickLuckyMoney(AccessibilityNodeInfo rootNode) {if(rootNode != null) {int count = rootNode.getChildCount();for (int i = count - 1; i >= 0; i--) { // 倒序查找最新的紅包AccessibilityNodeInfo node = rootNode.getChild(i);if (node == null)continue;CharSequence text = node.getText();if (text != null && text.toString().equals("領取紅包")) {AccessibilityNodeInfo parent = node.getParent();while (parent != null) {if (parent.isClickable()) {parent.performAction(AccessibilityNodeInfo.ACTION_CLICK);break;}parent = parent.getParent();}}clickLuckyMoney(node);}}}/*** 打開紅包*/private boolean openLuckyMoney() {AccessibilityNodeInfo rootNode = getRootInActiveWindow();if(rootNode != null) {List<AccessibilityNodeInfo> nodes =rootNode.findAccessibilityNodeInfosByViewId(UI.OPEN_LUCKY_MONEY_BUTTON_ID);for(AccessibilityNodeInfo node : nodes) {if(node.isClickable()) {Log.i(TAG, "open LuckyMoney");node.performAction(AccessibilityNodeInfo.ACTION_CLICK);return true;}}}return false;}private void backToHome() {if(handler.hasMessages(MSG_BACK_HOME)) {handler.removeMessages(MSG_BACK_HOME);}handler.sendEmptyMessage(MSG_BACK_HOME);}Handler handler = new Handler() {public void handleMessage(Message msg) {if(msg.what == MSG_BACK_HOME) {performGlobalAction(GLOBAL_ACTION_BACK);postDelayed(new Runnable() {public void run() {performGlobalAction(GLOBAL_ACTION_BACK);hasLuckyMoney = false;}}, 1500);} else if(msg.what == MSG_BACK_ONCE) {postDelayed(new Runnable() {public void run() {Log.i(TAG, "click back");performGlobalAction(GLOBAL_ACTION_BACK);hasLuckyMoney = false;hasNotify = false;}}, 1500);}}}; }ok,到這里就全部講完了,小伙伴們可以自己去實現更多更有趣、更新奇的功能了。這里我只是作為技術探索,容我再啰嗦兩點:
- 朋友是很重要的,有空的話還是好好回復吧
- 紅包只是一種噱頭,一種娛樂方式,別當作謀財之道喔
源碼下載
Source: http://www.jianshu.com/p/405330b2ecd8總結
以上是生活随笔為你收集整理的微信自动回复和自动抢红包实现原理(三):自动抢红包的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微信自动回复和自动抢红包实现原理(二):
- 下一篇: getSystemService() i