參考(感謝做者):http://www.jianshu.com/p/cd1cd53909d7java
http://blog.csdn.net/jiangwei0910410003/article/details/48895153node
實現原理android
經過利用AccessibilityService輔助服務,監測屏幕內容,如監聽狀態欄的信息,屏幕跳轉等,以此來實現自動拆紅包的功能。關於AccessibilityService輔助服務,能夠自行百度瞭解更多。git
代碼基礎:github
1.首先聲明一個RedPacketService繼承自AccessibilityService,該服務類有兩個方法必須重寫,以下:微信
/** * Created by cxk on 2017/2/3. * email:471497226@qq.com * * 搶紅包服務類 */ public class RedPacketService extends AccessibilityService { /** * 必須重寫的方法:此方法用了接受系統發來的event。在你註冊的event發生是被調用。在整個生命週期會被調用屢次。 */ @Override public void onAccessibilityEvent(AccessibilityEvent event) { }
/** * 必須重寫的方法:系統要中斷此service返回的響應時會調用。在整個生命週期會被調用屢次。 */ @Override public void onInterrupt() { Toast.makeText(this, "我快被終結了啊-----", Toast.LENGTH_SHORT).show(); }
/** * 服務已鏈接 */ @Override protected void onServiceConnected() { Toast.makeText(this, "搶紅包服務開啓", Toast.LENGTH_SHORT).show(); super.onServiceConnected(); }
/** * 服務已斷開 */ @Override public boolean onUnbind(Intent intent) { Toast.makeText(this, "搶紅包服務已被關閉", Toast.LENGTH_SHORT).show(); return super.onUnbind(intent); } }
2.對咱們的RedPacketService進行一些配置,這裏配置方法能夠選擇代碼動態配置(onServiceConnected裏配置),也能夠直接在res/xml下新建.xml文件,沒有xml文件夾就新建。這裏咱們將文件命名爲redpacket_service_config.xml,代碼以下:app
<?xml version="1.0" encoding="utf-8"?> <accessibility-service xmlns:android="http://schemas.android.com/apk/res/android" android:accessibilityEventTypes="typeAllMask" android:accessibilityFeedbackType="feedbackGeneric" android:accessibilityFlags="flagDefault" android:canRetrieveWindowContent="true" android:description="@string/desc" android:notificationTimeout="100" android:packageNames="com.tencent.mm" />
accessibilityEventTypes: ide
響應哪種類型的事件,typeAllMask就是響應全部類型的事件了,另外還有單擊、長按、滑動等。佈局
accessibilityFeedbackType: ui
用什麼方式反饋給用戶,有語音播出和振動。能夠配置一些TTS引擎,讓它實現發音。
packageNames:
指定響應哪一個應用的事件。這裏咱們是寫搶紅包助手,就寫微信的包名:com.tencent.mm,這樣就能夠監聽微信產生的事件了。
notificationTimeout:
響應時間
description:
輔助服務的描述信息。
3.service是四大組件之一,須要在AndroidManifest進行配置,注意這裏稍微有些不一樣:
<!--搶紅包服務--> <service android:name=".RedPacketService" android:enabled="true" android:exported="true" 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/redpacket_service_config"></meta-data> </service>
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE" 權限申請
android:resource="@xml/redpacket_service_config" 引用剛纔的配置文件
核心代碼:
咱們的紅包助手,核心思路分爲三步走:
監聽通知欄微信消息,若是彈出[微信紅包]字樣,模擬手指點擊狀態欄跳轉到微信聊天界面→在微信聊天界面查找紅包,若是找到則模擬手指點擊打開,彈出打開紅包界面→模擬手指點擊紅包「開」
1.監聽通知欄消息,查看是否有[微信紅包]字樣,代碼以下:
@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("[微信紅包]")) { //若是有則打開微信紅包頁面 openWeChatPage(event); } } } break; } } /** * 開啓紅包所在的聊天頁面 */ 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(); } } }
2.判斷當前是否在微信聊天頁面,是的話遍歷當前頁面各個控件,找到含有微信紅包或者領取紅包的textview控件,而後逐層找到他的可點擊父佈局(圖中綠色部分),模擬點擊跳轉到含有「開」的紅包界面,代碼以下:
@Override public void onAccessibilityEvent(AccessibilityEvent event) { int eventType = event.getEventType(); switch (eventType) { //窗口發生改變時會調用該事件 case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED: String className = event.getClassName().toString(); //判斷是不是微信聊天界面 if ("com.tencent.mm.ui.LauncherUI".equals(className)) { //獲取當前聊天頁面的根佈局 AccessibilityNodeInfo rootNode = getRootInActiveWindow(); //開始找紅包 findRedPacket(rootNode); } } } /** * 遍歷查找紅包 */ 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()) { //模擬點擊 parent.performAction(AccessibilityNodeInfo.ACTION_CLICK); //isOpenRP用於判斷該紅包是否點擊過 isOpenRP = true; break; } parent = parent.getParent(); } } //判斷是否已經打開過那個最新的紅包了,是的話就跳出for循環,不是的話繼續遍歷 if (isOpenRP) { break; } else { findRedPacket(node); } } } }
3.點擊紅包後,在模擬手指點擊「開」以此開啓紅包,跳轉到紅包詳情界面,方法與步驟二相似:
@Override public void onAccessibilityEvent(AccessibilityEvent event) { int eventType = event.getEventType(); switch (eventType) { //窗口發生改變時會調用該事件 case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED: String className = event.getClassName().toString(); //判斷是不是顯示‘開’的那個紅包界面 if ("com.tencent.mm.plugin.luckymoney.ui.LuckyMoneyReceiveUI".equals(className)) { AccessibilityNodeInfo rootNode = getRootInActiveWindow(); //開始搶紅包 openRedPacket(rootNode); } break; } } /** * 開始打開紅包 */ private void openRedPacket(AccessibilityNodeInfo rootNode) { for (int i = 0; i < rootNode.getChildCount(); i++) { AccessibilityNodeInfo node = rootNode.getChild(i); if ("android.widget.Button".equals(node.getClassName())) { node.performAction(AccessibilityNodeInfo.ACTION_CLICK); } openRedPacket(node); } }
結合以上三步,下面是完整代碼,註釋已經寫的很清楚,直接看代碼:
package com.cxk.redpacket; import android.accessibilityservice.AccessibilityService; import android.app.Instrumentation; import android.app.KeyguardManager; import android.app.Notification; import android.app.PendingIntent; import android.app.Service; import android.content.Context; import android.content.Intent; import android.os.IBinder; import android.os.PowerManager; import android.text.TextUtils; import android.util.Log; import android.view.KeyEvent; 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.LuckyMoneyReceiveUI"; /** * 用於判斷是否點擊過紅包了 */ 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("[微信紅包]")) { if (!isScreenOn()) { wakeUpScreen(); } //若是有則打開微信紅包頁面 openWeChatPage(event); isOpenRP = false; } } } break; //界面跳轉的監聽 case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED: String className = event.getClassName().toString(); //判斷是不是微信聊天界面 if (LAUCHER.equals(className)) { //獲取當前聊天頁面的根佈局 AccessibilityNodeInfo rootNode = getRootInActiveWindow(); //開始找紅包 findRedPacket(rootNode); } //判斷是不是顯示‘開’的那個紅包界面 if (LUCKEY_MONEY_RECEIVER.equals(className)) { AccessibilityNodeInfo rootNode = getRootInActiveWindow(); //開始搶紅包 openRedPacket(rootNode); } //判斷是不是紅包領取後的詳情界面 if (isOpenDetail && LUCKEY_MONEY_DETAIL.equals(className)) { isOpenDetail = false; //返回桌面 back2Home(); //若是以前是鎖着屏幕的則從新鎖回去 release(); } break; } } /** * 開始打開紅包 */ private void openRedPacket(AccessibilityNodeInfo rootNode) { for (int i = 0; i < rootNode.getChildCount(); i++) { AccessibilityNodeInfo node = rootNode.getChild(i); if ("android.widget.Button".equals(node.getClassName())) { 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()) { //模擬點擊 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; } } }
使用方法:
設置-輔助功能-無障礙-點擊RedPacket開啓便可(或者直接在設置搜索輔助功能or RedPacket)
注:由於AccessibilityService服務很容易斷開,因此咱們須要將咱們的App設置爲白名單,防止被系統KO掉。這樣他就能一直跑在咱們的後臺啦。
已知問題:
1.聊天列表或者聊天界面中沒法直接自動搶紅包
Demo下載地址:https://github.com/CKTim/RedPacket