AccessibilityService 進行螞蟻森林能量收集 demo
阿新 • • 發佈:2019-01-26
1. 原理
基於 AccessibilityService 模擬輔助功能
2. Demo下載
3. 跳轉支付寶登陸介面
我們也可以使用adb 命令 adb shell am start com.eg.android.AlipayGphone/com.eg.android.AlipayGphone.AlipayLogin
/**
* 啟動支付寶介面
* adb shell am start com.eg.android.AlipayGphone/com.eg.android.AlipayGphone.AlipayLogin
*/
public static void startAlipay(Context mContext) {
Intent intent = new Intent();
intent.setPackage("com.eg.android.AlipayGphone");
intent.setClassName("com.eg.android.AlipayGphone", "com.eg.android.AlipayGphone.AlipayLogin");
mContext.startActivity(intent);
}
4. 跳轉支付寶螞蟻森林介面
這裡的原理是:通過 findAccessibilityNodeInfosByText 找到螞蟻森林的節點,但是這個節點clikebale 屬性不可用,無法使用 AccessibilityNodeInfo.ACTION_CLICK,但是發現其父控制元件可以被點選,故對其父控制元件進行點選。
/**
* 自動點選進入螞蟻森林介面
*/
public static void enterForestUI(AccessibilityNodeInfo nodeInfo) {
Log.d(Config.TAG, "enterForestUI ");
if (nodeInfo != null) {
// 找到介面中螞蟻森林的文字
List<AccessibilityNodeInfo> list = nodeInfo.findAccessibilityNodeInfosByText("螞蟻森林");
if (list == null) {
Log.d(Config.TAG, "enterForestUI finding no");
return;
} else {
Log.d(Config.TAG, "enterForestUI finding yes");
}
for (AccessibilityNodeInfo item : list) {
/**
* 螞蟻森林本身不可點選,但是他的父控制元件可以點選
*/
AccessibilityNodeInfo parent = item.getParent();
if (null != parent && parent.isClickable()) {
parent.performAction(AccessibilityNodeInfo.ACTION_CLICK);
Log.d(Config.TAG, "item = " + item.toString() + ", parent click = " + parent.toString());
break;
}
}
}
}
5. 使用 AccessibilityService 點選能量球
能量球的button 事件 Button 的節點資料 text = null, descript = 4小時58分後才能收取, className = android.widget.Button, resId = null,匹配度比較低,故這裡要防止誤點選進入其他介面。
備註:好友的能量不能收取,因為支付寶在onTouch事件中return true,導致不會觸發OnClick方法,但是支付寶中的螞蟻森林可以收取自己的能量
public static void policy(AccessibilityNodeInfo nodeInfo, String packageName, String className) {
/**
* 螞蟻森林介面
*/
if (packageName.equals("com.eg.android.AlipayGphone") &&
("com.alipay.mobile.nebulacore.ui.H5Activity".equals(className)
|| "com.uc.webkit.bf".equals(className))) {
if (nodeInfo != null) {
for (int i = 0; i < nodeInfo.getChildCount(); i++) {
AccessibilityNodeInfo child = nodeInfo.getChild(i);
if ("com.uc.webview.export.WebView".equals(child.getClassName())) {
Log.d(Config.TAG, "找到螞蟻森林的 webView count = " + child.getChildCount());
findEveryViewNode(child);
break;
}
}
} else {
Log.d(Config.TAG, "alipayPolicy = null");
}
}
}
public static void findEveryViewNode(AccessibilityNodeInfo node) {
if (null != node && node.getChildCount() > 0) {
for (int i = 0; i < node.getChildCount(); i++) {
AccessibilityNodeInfo child = node.getChild(i);
// 有時 child 為空
if (child == null) {
continue;
}
//Log.d(TAG, "findEveryViewNode = " + child.toString());
String className = child.getClassName().toString();
if ("android.widget.Button".equals(className)) {
Log.d(Config.TAG, "Button 的節點資料 text = " + child.getText() + ", descript = " + child.getContentDescription() + ", className = " + child.getClassName() + ", resId = " + child.getViewIdResourceName());
boolean isClickable = child.isClickable();
boolean isResIdNull = child.getViewIdResourceName() == null ? true:false;
/**
* 好友的能量不能收取,因為支付寶在onTouch事件中return true,導致不會觸發OnClick方法
*
* 但是支付寶中的螞蟻森林可以收取自己的能量
*/
if ( isClickable && isResIdNull) {
child.performAction(AccessibilityNodeInfo.ACTION_CLICK);
Log.d(Config.TAG, "能量球 成功點選");
}
}
// 遞迴呼叫
findEveryViewNode(child);
}
}
}
執行日誌
2018-08-22 14:21:28.560 24942-24942/? D/suhuazhi: 找到螞蟻森林的 webView count = 1
2018-08-22 14:21:28.562 24942-24942/? D/suhuazhi: Button 的節點資料 text = null, descript = 4小時58分後才能收取, className = android.widget.Button, resId = null
2018-08-22 14:21:28.566 24942-24942/? D/suhuazhi: 能量球 成功點選
2018-08-22 14:21:28.571 24942-24942/? D/suhuazhi: Button 的節點資料 text = null, descript = 道具, className = android.widget.Button, resId = J_alter_game_tools
2018-08-22 14:21:28.577 24942-24942/? D/suhuazhi: Button 的節點資料 text = null, descript = 攻略, className = android.widget.Button, resId = J_alter_tips
2018-08-22 14:21:28.577 24942-24942/? D/suhuazhi: Button 的節點資料 text = null, descript = 訊息, className = android.widget.Button, resId = J_message_entry
2018-08-22 14:21:28.578 24942-24942/? D/suhuazhi: Button 的節點資料 text = null, descript = 合種, className = android.widget.Button, resId = J_cooperation_entry