Manifest文件中配置service
在res下创建xml目录 创建accessibility.xml 配置service
之后在MainActivity中 查看一下是否开启无障碍模式,如果没有提醒用户去开启 当配置完毕之后 service将会自动启动
service中代码
public class MyAccessibilityService extends AccessibilityService {
@Override
protected void onServiceConnected() {
LogUtils.d("onServiceConnected");
}
String description;
ArrayList topList = new ArrayList<>();
List lvs;
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
try {
//微信UI界面的根节点,开始遍历节点
AccessibilityNodeInfo rootNodeInfo = getRootInActiveWindow();
if (rootNodeInfo == null) {
return;
}
description = "";
if (rootNodeInfo.getContentDescription() != null) {
description = rootNodeInfo.getContentDescription().toString();
}
//自动点赞流程
if (mUserName.equals("")) {
//Lv
lvs = rootNodeInfo.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/eii");
LogUtils.d("找到的Lv数量: " + lvs.size());
//如果size不为0,证明当前在朋友圈页面下,开始执行逻辑
if (lvs.size() != 0) {
//1.先记录用户名
List userNames =
rootNodeInfo.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/b6e");
LogUtils.d("用户名数量: " + userNames.size());
if (userNames.size() != 0) {
LogUtils.d("getParent:" + userNames.get(0).getParent());
LogUtils.d("getChildCount:" + userNames.get(0).getParent().getChildCount());
// && userNames.get(0).getParent().getChildCount() == 4
if (userNames.get(0).getParent() != null ) {
mUserName = userNames.get(0).getText().toString();
if (!mUserName.equals("") && !ifOnce) {
LogUtils.d("初始化,只会执行一次");
LogUtils.d("当前的用户名:" + mUserName);
ifOnce = true;
//测试朋友圈点赞
test3(rootNodeInfo);
}
}
}
} else {
ifOnce = false;
mUserName = "";
}
}
} catch (Exception e) {
if (e != null && e.getMessage() != null) {
LogUtils.d("报错:" + e.getMessage().toString());
}
}
}
String mUserName = "";
private boolean ifOnce = false;
/**
* com.tencent.mm:id/cn0
* 朋友圈点赞 (目前实现手动滚动全部点赞)
* 上方固定显示的名字:com.tencent.mm:id/afa
* 下方点赞:显示id:com.tencent.mm:id/cnn
* 每发现一个【评论按钮】,就去搜索当前同父组件下的点赞区域有没有自己的ID。
* 如果有就不点赞,如果没有就点赞
* 这里要改成不通过Id抓取提高稳定性
*
* @param rootNodeInfo
*/
private synchronized void test3(AccessibilityNodeInfo rootNodeInfo) {
LogUtils.d("当前线程:" + Thread.currentThread());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
topList.clear();
if (!mUserName.equals("")) {
//测试获得评论按钮的父节点,再反推出点赞按钮
List fuBtns =
rootNodeInfo.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/emw");
LogUtils.d("fuBtns数量:" + fuBtns.size());
if (fuBtns.size() != 0) {
//删掉超出屏幕的fuBtn
AccessibilityNodeInfo lastFuBtn = fuBtns.get(fuBtns.size() - 1);
Rect lastFuBtnOutBound = new Rect();
lastFuBtn.getBoundsInScreen(lastFuBtnOutBound);
WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics dm = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels; // 屏幕宽度(像素)
int height = dm.heightPixels; // 屏幕高度(像素)
float density = dm.density; // 屏幕密度(0.75 / 1.0 / 1.5)
int densityDpi = dm.densityDpi; // 屏幕密度dpi(120 / 160 / 240)
// 屏幕宽度算法:屏幕宽度(像素)/屏幕密度
int screenWidth = (int) (width / density); // 屏幕宽度(dp)
int screenHeight = (int) (height / density);// 屏幕高度(dp)
if (lastFuBtnOutBound.top > height) {
fuBtns.remove(lastFuBtn);
}
for (int i = 0; i < fuBtns.size(); i++) {
AccessibilityNodeInfo fuBtn = fuBtns.get(i);
LogUtils.d("fuBtn的子节点数量:" + fuBtn.getChildCount());//3-4个
List plBtns = fuBtn.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/eho");
LogUtils.d("从这里发现评论按钮:" + plBtns.size());
if (plBtns.size() == 0) {
if (lvs.get(0).performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD)) {
test3(getRootInActiveWindow());
}
return;
}
AccessibilityNodeInfo plbtn = plBtns.get(0); //评论按钮
List zanBtns = fuBtn.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/emj");
LogUtils.d("从这里发现点赞文字显示区域:" + zanBtns.size());
if (zanBtns.size() != 0) {
//2.如果不为空,则查找有没有自己点过赞,有则不点,没有则点
AccessibilityNodeInfo zanbtn = zanBtns.get(0);
LogUtils.d("点赞的人是:" + zanbtn.getText().toString());
if (zanbtn != null && zanbtn.getText() != null &&
zanbtn.getText().toString().contains(mUserName)) {
LogUtils.d("*********************这一条已经被赞过辣");
//判断是否需要翻页,如果当前所有页面的父节点都没点过了,就需要翻页
boolean ifxuyaofanye = false;
LogUtils.d("O(≧口≦)O: i=" + i + " fuBtns.size():" + fuBtns.size());
if (i == fuBtns.size() - 1) {
ifxuyaofanye = true;
}
if (ifxuyaofanye) {
//滑动前检测一下是否还有没有点过的点
if (jianceIfLou()) {
LogUtils.d("还有遗漏的点!!!!再检查一遍!!!!!!!!!!");
test3(getRootInActiveWindow());
} else {
if (lvs.get(0).performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD)) {
test3(getRootInActiveWindow());
return;
}
}
}
} else {
LogUtils.d("**************************:自己没有赞过!");
//开始执行点赞流程
if (plBtns.size() != 0) {
Rect outBounds = new Rect();
plbtn.getBoundsInScreen(outBounds);
int top = outBounds.top;
//根据top判断如果已经点开了就不重复点开了
if (topList.contains(top)) {
return;
}
//com.tencent.mm:id/cj5 赞
if (plbtn.performAction(AccessibilityNodeInfo.ACTION_CLICK)) {
List zanlBtns = rootNodeInfo.
findAccessibilityNodeInfosByViewId("com.tencent.mm:id/eh_");
if (zanlBtns.size() != 0) {
if (!topList.contains(top) && zanlBtns.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK)) {
topList.add(top);
LogUtils.d("topList:" + topList.toString());
//判断是否需要翻页,如果当前所有页面的父节点都没点过了,就需要翻页
boolean ifxuyaofanye = false;
LogUtils.d("O(≧口≦)O: i=" + i + " fuBtns.size():" + fuBtns.size());
if (i == fuBtns.size() - 1) {
ifxuyaofanye = true;
}
if (ifxuyaofanye) {
//滑动前检测一下是否还有没有点过的点
if (jianceIfLou()) {
LogUtils.d("还有遗漏的点!!!!再检查一遍!!!!!!!!!!");
test3(getRootInActiveWindow());
} else {
if (lvs.get(0).performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD)) {
test3(getRootInActiveWindow());
return;
}
}
}
}
}
}
}
}
} else {
LogUtils.d("**************************:点赞区域为空!plBtns.size() :" + plBtns.size());
//开始执行点赞流程
if (plBtns.size() != 0) {
Rect outBounds = new Rect();
plbtn.getBoundsInScreen(outBounds);
int top = outBounds.top;
//根据top判断如果已经点开了就不重复点开了
if (topList.contains(top)) {
return;
}
//com.tencent.mm:id/cj5 赞
if (plbtn.performAction(AccessibilityNodeInfo.ACTION_CLICK)) {
List zanlBtns = rootNodeInfo.
findAccessibilityNodeInfosByViewId("com.tencent.mm:id/eh_");
if (zanlBtns.size() != 0) {
if (!topList.contains(top) && zanlBtns.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK)) {
topList.add(top);
LogUtils.d("topList:" + topList.toString());
//判断是否需要翻页,如果当前所有页面的父节点都没点过了,就需要翻页
boolean ifxuyaofanye = false;
LogUtils.d("O(≧口≦)O: i=" + i + " fuBtns.size():" + fuBtns.size());
if (i == fuBtns.size() - 1) {
ifxuyaofanye = true;
}
if (ifxuyaofanye) {
//滑动前检测一下是否还有没有点过的点
if (jianceIfLou()) {
LogUtils.d("还有遗漏的点!!!!再检查一遍!!!!!!!!!!");
test3(getRootInActiveWindow());
} else {
if (lvs.get(0).performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD)) {
test3(getRootInActiveWindow());
return;
}
}
}
}
}
}
}
}
}
}
}
}
private boolean jianceIfLou() {
boolean result = false;
List fuBtns =
getRootInActiveWindow().findAccessibilityNodeInfosByViewId("com.tencent.mm:id/co0");
LogUtils.d("检查的父节点数量:" + fuBtns.size());
if (fuBtns.size() != 0) {
for (AccessibilityNodeInfo fuBtn : fuBtns) {
//点赞区域
List zanBtns = fuBtn.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/emj");
LogUtils.d("检查的父节点的点赞区域数量:" + zanBtns.size());
if (zanBtns.size() != 0) {
AccessibilityNodeInfo zanbtn = zanBtns.get(0);
LogUtils.d(" zanbtn.getText().toString():" + zanbtn.getText().toString());
if (zanbtn != null && zanbtn.getText() != null &&
zanbtn.getText().toString().contains(mUserName)) {
result = false;
} else {
result = true;
}
} else {
result = true;
}
}
}
return result;
}
@Override
public void onInterrupt() {
LogUtils.d("onInterrupt");
}
}
“com.tencent.mm:id/eii”这个id是对微信页面抓取的id 如果不了解怎么抓取 详情见monitor抓取 每次微信更新 这些id基本都会变,需要进行适配
eii:朋友圈列表整体list ID
b6e:列表用户名 ID
eho:评论点赞父及按钮
emj:已点赞人条目
eh_:点赞按钮
完整代码 :https://download.csdn.net/download/smallredzi/11138891