package com.from206.myapplication;
import android.accessibilityservice.AccessibilityService;
import android.content.Intent;
import android.os.Binder;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.Toast;
import java.util.List;
public class WeChatLogService extends AccessibilityService {
/**
* 聊天对象
*/
private String ChatName;
/**
* 聊天最新一条记录
*/
private String ChatRecord = "test";
private static final String TAG = "WeChatLogService";
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
int eventType = event.getEventType();
switch (eventType) {
case AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED:
//获取当前聊天页面的根布局
AccessibilityNodeInfo rootNode = getRootInActiveWindow();
//获取聊天信息
getWeChatLog2(rootNode);
break;
//当前页面
case AccessibilityEvent.TYPE_VIEW_SCROLLED:
//获取当前聊天页面的根布局
AccessibilityNodeInfo rootNode2 = getRootInActiveWindow();
//获取聊天信息
getWeChatLog(rootNode2);
break;
//通知栏
case AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED:
List<CharSequence> texts = event.getText();
if (!texts.isEmpty()) {
for (CharSequence text : texts) {
String content = text.toString();
if (content.contains(": ") && content.split(": ").length == 2) {
Log.e(TAG, "有新消息 " + content);
}
// if (content.contains("[微信红包]")) {
// //模拟打开通知栏消息,即打开微信
// if (event.getParcelableData() != null &&
// event.getParcelableData() instanceof Notification) {
// Notification notification = (Notification) event.getParcelableData();
// PendingIntent pendingIntent = notification.contentIntent;
// try {
// pendingIntent.send();
// Log.e("demo","进入微信");
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
// }
}
}
break;
}
}
/**
* 在微信主页面
* @param rootNode
*/
private void getWeChatLog2(AccessibilityNodeInfo rootNode) {
if(rootNode != null){
List<AccessibilityNodeInfo> listChatRecord = rootNode.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/aft");
if (listChatRecord.size() == 0) {
return;
}
//获取最后一行聊天的线性布局(即是最新的那条消息)
AccessibilityNodeInfo firstNode = listChatRecord.get(0);
List<AccessibilityNodeInfo> nickName = firstNode.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/afv");
List<AccessibilityNodeInfo> record = firstNode.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/afx");
if (nickName.size() != 0) {
if (record.size() == 0) {
//判断当前这条消息是不是和上一条一样,防止重复
if (!ChatRecord.equals("对方发的是图片或者表情")) {
//获取聊天对象
ChatName = nickName.get(0).getText().toString();
//获取聊天信息
ChatRecord = "对方发的是图片或者表情";
Log.e(TAG, ChatName + ":" + "对方发的是图片或者表情");
Toast.makeText(this, ChatName + ":" + ChatRecord, Toast.LENGTH_SHORT).show();
}
} else {
//判断当前这条消息是不是和上一条一样,防止重复
if (!ChatRecord.equals(record.get(0).getText().toString())) {
//获取聊天对象
ChatName = nickName.get(0).getText().toString();
//获取聊天信息
ChatRecord = record.get(0).getText().toString();
Log.e(TAG, ChatName + ":" + ChatRecord);
Toast.makeText(this, ChatName + ":" + ChatRecord, Toast.LENGTH_SHORT).show();
}
}
}
}
}
/**
* 遍历
*
* @param rootNode
*/
private void getWeChatLog(AccessibilityNodeInfo rootNode) {
if (rootNode != null) {
//获取所有聊天的线性布局
List<AccessibilityNodeInfo> listChatRecord = rootNode.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/p");
if (listChatRecord.size() == 0) {
return;
}
//获取最后一行聊天的线性布局(即是最新的那条消息)
AccessibilityNodeInfo finalNode = listChatRecord.get(listChatRecord.size() - 1);
//获取聊天对象list(其实只有size为1)
List<AccessibilityNodeInfo> imageName = finalNode.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/id");
//获取聊天信息list(其实只有size为1)
List<AccessibilityNodeInfo> record = finalNode.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/if");
if (imageName.size() != 0) {
if (record.size() == 0) {
//判断当前这条消息是不是和上一条一样,防止重复
if (!ChatRecord.equals("对方发的是图片或者表情")) {
//获取聊天对象
ChatName = imageName.get(0).getContentDescription().toString().replace("头像", "");
//获取聊天信息
ChatRecord = "对方发的是图片或者表情";
Log.e(TAG, ChatName + ":" + "对方发的是图片或者表情");
Toast.makeText(this, ChatName + ":" + ChatRecord, Toast.LENGTH_SHORT).show();
}
} else {
//判断当前这条消息是不是和上一条一样,防止重复
if (!ChatRecord.equals(record.get(0).getText().toString())) {
//获取聊天对象
ChatName = imageName.get(0).getContentDescription().toString().replace("头像", "");
//获取聊天信息
ChatRecord = record.get(0).getText().toString();
Log.e(TAG, ChatName + ":" + ChatRecord);
Toast.makeText(this, ChatName + ":" + ChatRecord, Toast.LENGTH_SHORT).show();
}
}
}
}
}
/**
* 必须重写的方法:系统要中断此service返回的响应时会调用。在整个生命周期会被调用多次。
*/
@Override
public void onInterrupt() {
Toast.makeText(this, "我快被终结了啊-----", Toast.LENGTH_SHORT).show();
}
/**
* 服务开始连接
*/
@Override
protected void onServiceConnected() {
Toast.mak