阅读这篇文章前,你需要先了解AccessibilityService,可以先阅读我上一篇文章
微信自动回复和自动抢红包实现原理(一):AccessibilityService的介绍和配置
已经了解的朋友可以直接阅读该文章
完成AccessibilityService的配置后,好像无从下手。先别急,先打印一些log看看吧。把下面的方法放在onAccessibilityEvent()里:
private void printEventLog(AccessibilityEvent event) {
Log.i(TAG, "-------------------------------------------------------------");
int eventType = event.getEventType(); //事件类型
Log.i(TAG, "PackageName:" + event.getPackageName() + ""); // 响应事件的包名
Log.i(TAG, "Source Class:" + event.getClassName() + ""); // 事件源的类名
Log.i(TAG, "Description:" + event.getContentDescription()+ ""); // 事件源描述
Log.i(TAG, "Event Type(int):" + eventType + "");
switch (eventType) {
case AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED:// 通知栏事件
Log.i(TAG, "event type:TYPE_NOTIFICATION_STATE_CHANGED");
break;
case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED://窗体状态改变
Log.i(TAG, "event type:TYPE_WINDOW_STATE_CHANGED");
break;
case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED://View获取到焦点
Log.i(TAG, "event type:TYPE_VIEW_ACCESSIBILITY_FOCUSED");
break;
case AccessibilityEvent.TYPE_GESTURE_DETECTION_START:
Log.i(TAG, "event type:TYPE_VIEW_ACCESSIBILITY_FOCUSED");
break;
case AccessibilityEvent.TYPE_GESTURE_DETECTION_END:
Log.i(TAG, "event type:TYPE_GESTURE_DETECTION_END");
break;
case AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED:
Log.i(TAG, "event type:TYPE_WINDOW_CONTENT_CHANGED");
break;
case AccessibilityEvent.TYPE_VIEW_CLICKED:
Log.i(TAG, "event type:TYPE_VIEW_CLICKED");
break;
case AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED:
Log.i(TAG, "event type:TYPE_VIEW_TEXT_CHANGED");
break;
case AccessibilityEvent.TYPE_VIEW_SCROLLED:
Log.i(TAG, "event type:TYPE_VIEW_SCROLLED");
break;
case AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED:
Log.i(TAG, "event type:TYPE_VIEW_TEXT_SELECTION_CHANGED");
break;
default:
Log.i(TAG, "no listen event");
}
for (CharSequence txt : event.getText()) {
Log.i(TAG, "text:" + txt);
}
Log.i(TAG, "-------------------------------------------------------------");
}
向安装了服务的手机发微信信息,查看打印的log:
非锁屏(在后台):
-------------------------------------------------------------
packageName:com.tencent.mm
source:null
source class:android.app.Notification
event type(int):64
event type:TYPE_NOTIFICATION_STATE_CHANGED
text:[联系人]: 哦
-------------------------------------------------------------
非锁屏(在前台主界面):
-------------------------------------------------------------
packageName:com.tencent.mm
source:android.view.accessibility.AccessibilityNodeInfo@8009b539; boundsInParent: Rect(0, 0 - 38, 38); boundsInScreen: Rect(103, 1181 - 141, 1219); packageName: com.tencent.mm; className: android.widget.TextView; text: 1; error: null; maxTextLength: -1; contentDescription: null; viewIdResName: null; checkable: false; checked: false; focusable: false; focused: false; selected: false; clickable: false; longClickable: false; enabled: true; password: false; scrollable: false; actions: [AccessibilityAction: ACTION_SELECT -