微信紅包不錯的分析: 附帶源碼 並包含了源碼
(1) https://www.jianshu.com/p/19ddd41aa349
(2) http://blog.csdn.net/zero_zero_zero_zero/article/details/52008009
這個的源碼和解釋已經吃透了:
(3)http://www.cnblogs.com/cxk1995/p/6363574.html
只需要把
// private String LUCKEY_MONEY_RECEIVER = "com.tencent.mm.plugin.luckymoney.ui.LuckyMoneyReceiveUI";
換成自己的:
private String LUCKEY_MONEY_RECEIVER = "com.tencent.mm.plugin.luckymoney.ui.En_fba4b94f";
或者:加上: private String LUCK_MONEY_RECEIVE_PREFIX="com.tencent.mm.plugin.luckymoney.ui";
然后:把
// Log.e("spire==測試 哪個是開的ui名稱:", className); 由於微信經常 變換開頁面的ui名稱,此處直接找到微信頭部,進入 if(className.equals(LUCKEY_MONEY_RECEIVE)){ // if (LUCKEY_MONEY_RECEIVER.equals(className)) { AccessibilityNodeInfo rootNode = getRootInActiveWindow(); //開始搶紅包 Log.e("spire點擊了 之后紅包進入 開的界面", className); openRedPacket(rootNode); }
改成:
//判斷是否是顯示‘開’的那個紅包界面
//判斷是否是顯示‘開’的那個紅包界面 // Log.e("spire==測試 哪個是開的ui名稱:", className); 由於微信經常 變換開頁面的ui名稱,此處直接找到微信頭部,進入 if(className.indexOf(LUCK_MONEY_RECEIVE_PREFIX)==0 && (!className.equals(LUCKEY_MONEY_DETAIL))){ // if (LUCKEY_MONEY_RECEIVER.equals(className)) { AccessibilityNodeInfo rootNode = getRootInActiveWindow(); //開始搶紅包 Log.e("spire點擊了 之后紅包進入 開的界面", className); openRedPacket(rootNode); }
這是完整的代碼:
package com.cxk.redpacket; import android.accessibilityservice.AccessibilityService; import android.app.KeyguardManager; import android.app.Notification; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.PowerManager; import android.text.TextUtils; import android.util.Log; import android.view.accessibility.AccessibilityEvent; import android.view.accessibility.AccessibilityNodeInfo; import android.widget.Toast; import java.util.List; /** * 搶紅包Service,繼承AccessibilityService */ public class RedPacketService extends AccessibilityService { /** * 微信幾個頁面的包名+地址。用於判斷在哪個頁面 * LAUCHER-微信聊天界面 * LUCKEY_MONEY_RECEIVER-點擊紅包彈出的界面 * LUCKEY_MONEY_DETAIL-紅包領取后的詳情界面 */ private String LAUCHER = "com.tencent.mm.ui.LauncherUI"; private String LUCKEY_MONEY_DETAIL = "com.tencent.mm.plugin.luckymoney.ui.LuckyMoneyDetailUI"; // private String LUCKEY_MONEY_RECEIVER = "com.tencent.mm.plugin.luckymoney.ui.LuckyMoneyReceiveUI1"; // private String LUCKEY_MONEY_RECEIVER = "com.tencent.mm.plugin.luckymoney.ui.En_fba4b94f"; private String LUCK_MONEY_RECEIVE_PREFIX="com.tencent.mm.plugin.luckymoney.ui"; /** * 用於判斷是否點擊過紅包了 */ private boolean isOpenRP; private boolean isOpenDetail = false; /** * 用於判斷是否屏幕是亮着的 */ private boolean isScreenOn; /** * 獲取PowerManager.WakeLock對象 */ private PowerManager.WakeLock wakeLock; /** * KeyguardManager.KeyguardLock對象 */ private KeyguardManager.KeyguardLock keyguardLock; @Override public void onAccessibilityEvent(AccessibilityEvent event) { int eventType = event.getEventType(); switch (eventType) { //通知欄來信息,判斷是否含有微信紅包字樣,是的話跳轉 case AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED: List<CharSequence> texts = event.getText(); for (CharSequence text : texts) { String content = text.toString(); if (!TextUtils.isEmpty(content)) { //判斷是否含有[微信紅包]字樣 if (content.contains("[微信紅包]")) { Log.e("spire發現有紅包的通知 [微信紅包]", content); if (!isScreenOn()) { wakeUpScreen(); } //如果有則打開微信紅包頁面 openWeChatPage(event); isOpenRP = false; } } } break; //界面跳轉的監聽 case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED: String className = event.getClassName().toString(); //判斷是否是微信聊天界面 if (LAUCHER.equals(className)) { Log.e("spire 進入列表頁 找紅包", className); //獲取當前聊天頁面的根布局 AccessibilityNodeInfo rootNode = getRootInActiveWindow(); //開始找紅包 findRedPacket(rootNode); } //判斷是否是顯示‘開’的那個紅包界面 // Log.e("spire==測試 哪個是開的ui名稱:", className); 由於微信經常 變換開頁面的ui名稱,此處直接找到微信頭部,進入 if(className.indexOf(LUCK_MONEY_RECEIVE_PREFIX)==0 && (!className.equals(LUCKEY_MONEY_DETAIL))){ // if (LUCKEY_MONEY_RECEIVER.equals(className)) { AccessibilityNodeInfo rootNode = getRootInActiveWindow(); //開始搶紅包 Log.e("spire點擊了 之后紅包進入 開的界面", className); openRedPacket(rootNode); } //判斷是否是紅包領取后的詳情界面 if (isOpenDetail && LUCKEY_MONEY_DETAIL.equals(className)) { Log.e("spire領取之后的詳細界面", className); isOpenDetail = false; //返回桌面 back2Home(); //如果之前是鎖着屏幕的則重新鎖回去 release(); } break; } } /** * 開始打開紅包 */ private void openRedPacket(AccessibilityNodeInfo rootNode) { if (rootNode != null) { for (int i = 0; i < rootNode.getChildCount(); i++) { AccessibilityNodeInfo node = rootNode.getChild(i); Log.e("spire openRedPacket==", node.getClassName().toString()); if ("android.widget.Button".equals(node.getClassName())) { Log.e("spire 成功點開le ", node.getClassName().toString()); node.performAction(AccessibilityNodeInfo.ACTION_CLICK); isOpenDetail = true; } openRedPacket(node); } } } /** * 遍歷查找紅包 */ private void findRedPacket(AccessibilityNodeInfo rootNode) { if (rootNode != null) { //從最后一行開始找起 for (int i = rootNode.getChildCount() - 1; i >= 0; i--) { AccessibilityNodeInfo node = rootNode.getChild(i); //如果node為空則跳過該節點 if (node == null) { continue; } CharSequence text = node.getText(); if (text != null && text.toString().equals("領取紅包")) { AccessibilityNodeInfo parent = node.getParent(); //while循環,遍歷"領取紅包"的各個父布局,直至找到可點擊的為止 while (parent != null) { if (parent.isClickable()) { //模擬點擊 Log.e("spire發現了列表頁的紅包 執行點擊", text.toString()); parent.performAction(AccessibilityNodeInfo.ACTION_CLICK); //isOpenRP用於判斷該紅包是否點擊過 isOpenRP = true; break; } parent = parent.getParent(); } } //判斷是否已經打開過那個最新的紅包了,是的話就跳出for循環,不是的話繼續遍歷 if (isOpenRP) { break; } else { findRedPacket(node); } } } } /** * 開啟紅包所在的聊天頁面 */ private void openWeChatPage(AccessibilityEvent event) { //A instanceof B 用來判斷內存中實際對象A是不是B類型,常用於強制轉換前的判斷 if (event.getParcelableData() != null && event.getParcelableData() instanceof Notification) { Notification notification = (Notification) event.getParcelableData(); //打開對應的聊天界面 PendingIntent pendingIntent = notification.contentIntent; try { pendingIntent.send(); } catch (PendingIntent.CanceledException e) { e.printStackTrace(); } } } /** * 服務連接 */ @Override protected void onServiceConnected() { Toast.makeText(this, "搶紅包服務開啟", Toast.LENGTH_SHORT).show(); super.onServiceConnected(); } /** * 必須重寫的方法:系統要中斷此service返回的響應時會調用。在整個生命周期會被調用多次。 */ @Override public void onInterrupt() { Toast.makeText(this, "我快被終結了啊-----", Toast.LENGTH_SHORT).show(); } /** * 服務斷開 */ @Override public boolean onUnbind(Intent intent) { Toast.makeText(this, "搶紅包服務已被關閉", Toast.LENGTH_SHORT).show(); return super.onUnbind(intent); } /** * 返回桌面 */ private void back2Home() { Intent home = new Intent(Intent.ACTION_MAIN); home.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); home.addCategory(Intent.CATEGORY_HOME); startActivity(home); } /** * 判斷是否處於亮屏狀態 * * @return true-亮屏,false-暗屏 */ private boolean isScreenOn() { PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); isScreenOn = pm.isScreenOn(); Log.e("isScreenOn", isScreenOn + ""); return isScreenOn; } /** * 解鎖屏幕 */ private void wakeUpScreen() { //獲取電源管理器對象 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); //后面的參數|表示同時傳入兩個值,最后的是調試用的Tag wakeLock = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.FULL_WAKE_LOCK, "bright"); //點亮屏幕 wakeLock.acquire(); //得到鍵盤鎖管理器 KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE); keyguardLock = km.newKeyguardLock("unlock"); //解鎖 keyguardLock.disableKeyguard(); } /** * 釋放keyguardLock和wakeLock */ public void release() { if (keyguardLock != null) { keyguardLock.reenableKeyguard(); keyguardLock = null; } if (wakeLock != null) { wakeLock.release(); wakeLock = null; } } }