Android 輔助功能 -搶紅包(三)
本篇文章繼續講述輔助功能.
主要透過監聽通知欄紅包訊息,來跳轉聊天頁面,並自動回覆對方"謝謝".
上篇文章我們講述了監聽notification, 跳轉聊天介面. 具體可檢視:
Android 輔助功能 -搶紅包(二)
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 釋出!