Android 輔助功能 -搶紅包(三) 本篇文章繼續講述輔助功能. 主要通過監聽通知欄紅包消息,來跳轉聊天頁面,並自動回覆對方"謝謝". 上篇文章我們講述了監聽notification, 跳轉聊天界面. 具體可查看: Android 輔助功能 -搶紅包(二) 1: 使用monitor抓取id. 打 ...
Android 輔助功能 -搶紅包(三)
本篇文章繼續講述輔助功能.
主要通過監聽通知欄紅包消息,來跳轉聊天頁面,並自動回覆對方"謝謝".
上篇文章我們講述了監聽notification, 跳轉聊天界面. 具體可查看:
1: 使用monitor抓取id.
打開android device monitor 獲取resouce-id 備用.
2: 設置聊天框文本
通過步驟1獲取到的resouce-id來獲取AccessibilityNodeInfo.
List<AccessibilityNodeInfo> accessibilityNodeInfosByViewId = rootInActiveWindow.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/bkk");
找到edit結點後, 設置edit文本內容,具體方法為performAction(AccessibilityNodeInfo.ACTION_SET_TEXT, Bundle).
Bundle arguments = new Bundle();
arguments.putCharSequence(AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE, "謝謝");
accessibilityNodeInfo.performAction(AccessibilityNodeInfo.ACTION_SET_TEXT, arguments);
3: 點擊發送
List<AccessibilityNodeInfo> accessibilityNodeInfosByViewId = rootInActiveWindow.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/bql");
for (AccessibilityNodeInfo accessibilityNodeInfo : accessibilityNodeInfosByViewId) {
accessibilityNodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
step = 0;
}
仍然通過findAccessibilityNodeInfosByViewId找到發送按鈕的結點, 調用AccessibilityNodeInfo.ACTION_CLICK 發送文本.
完整的代碼如下:
public class RedPacketAccessibilityService extends AccessibilityService {
private static final String TAG = "RedPacketAccessibilityService";
private int step = 0;
@SuppressLint("LongLogTag")
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
int action = event.getAction();
Log.i(TAG, "onAccessibilityEvent: " + action);
CharSequence packageName = event.getPackageName();
Log.i(TAG, "onAccessibilityEvent: " + packageName);
AccessibilityNodeInfo rootInActiveWindow = getRootInActiveWindow();
if (rootInActiveWindow != null) {
// List<AccessibilityNodeInfo> nodeInfos = rootInActiveWindow.findAccessibilityNodeInfosByText("紅包:恭喜發財");
// if (nodeInfos != null) {
// for (AccessibilityNodeInfo nodeInfo : nodeInfos) {
// nodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
// }
// }
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
//第二步驟: 設置文本edit內容
if (step == 1) {
List<AccessibilityNodeInfo> accessibilityNodeInfosByViewId = rootInActiveWindow.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/bkk");
if (accessibilityNodeInfosByViewId != null) {
Log.i(TAG, "onAccessibilityEvent: 查找edit");
for (AccessibilityNodeInfo accessibilityNodeInfo : accessibilityNodeInfosByViewId) {
Bundle arguments = new Bundle();
arguments.putCharSequence(AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE, "謝謝");
accessibilityNodeInfo.performAction(AccessibilityNodeInfo.ACTION_SET_TEXT, arguments);
step = 2;
}
}
}
}
}
Log.i(TAG, "onAccessibilityEvent: step =" + step);
//點擊發送按鈕,發送謝謝
if (step == 2) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
List<AccessibilityNodeInfo> accessibilityNodeInfosByViewId = rootInActiveWindow.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/bql");
for (AccessibilityNodeInfo accessibilityNodeInfo : accessibilityNodeInfosByViewId) {
accessibilityNodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
step = 0;
}
}
}
switch (event.getEventType()) {
case AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED:
List<CharSequence> text = event.getText();
for (CharSequence charSequence : text) {
Log.i(TAG, "onAccessibilityEvent: notification ->" + charSequence);
// if (charSequence.toString().contains("請求添加") ||
// charSequence.toString().contains("[微信紅包]"))
if (charSequence.toString().contains("[微信紅包]")) {
step = 0; //重置
Parcelable parcelableData = event.getParcelableData();
if (parcelableData != null && parcelableData instanceof Notification) {
PendingIntent contentIntent = ((Notification) parcelableData).contentIntent;
new Handler().postDelayed(() -> {
try {
//第一步:跳轉聊天界面
contentIntent.send();
step = 1;
} catch (PendingIntent.CanceledException e) {
Log.e(TAG, "onAccessibilityEvent: ", e);
}
}, 1000);
}
}
}
break;
case AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED:
List<CharSequence> text1 = event.getText();
for (CharSequence charSequence : text1) {
Log.i(TAG, "onAccessibilityEvent: TYPE_VIEW_TEXT_CHANGED ->" + charSequence);
}
break;
case AccessibilityEvent.TYPE_VIEW_CLICKED:
List<CharSequence> text2 = event.getText();
for (CharSequence charSequence : text2) {
Log.i(TAG, "onAccessibilityEvent: TYPE_VIEW_CLICKED ->" + charSequence);
}
break;
}
}
@Override
public void onInterrupt() {
}
}
本文由博客一文多發平臺 OpenWrite 發佈!