用到的技术和工具AccessibilityService和uiautomatorviewer.
设备要求:android7.0以上 因为AccessibilityService在7.0以后才支持手势操作
抖音版本5.8.0(因为版本不同view的id也不同)
AccessibilityService:实现自动化操作的系统服务. 参考博客:(AccessibilityService) Android 辅助功能笔记
uiautomatorviewer:找到页面ID的插件,在android的SDK里面:Android\Sdk\tools\bin\uiautomatorviewer.bat
cj.png获取权限:在res-->xml下新建xml文件
android:canPerformGestures="true" 允许进行手势操作
android:canRetrieveWindowContent="true" 允许读取页面上的数据
<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeAllMask"
android:accessibilityFeedbackType="feedbackGeneric"
android:accessibilityFlags="flagDefault"
android:canPerformGestures="true"
android:canRetrieveWindowContent="true"
android:notificationTimeout="100" />
按钮点击事件(View必须是可点击的clickable=true,在uiautomatorviewer中可查看View的属性)
/**
* 根据ID点击某个视图
*/
public void perforGlobalClick(String id) {
AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
if (nodeInfo != null) {
List<AccessibilityNodeInfo> list = nodeInfo.findAccessibilityNodeInfosByViewId(id);
nodeInfo.recycle();
for (AccessibilityNodeInfo item : list) {
item.performAction(AccessibilityNodeInfo.ACTION_CLICK);
}
}
}
获取TextView的文本
/**
* 获取指定ID的文本
*/
private String getTextById(String id) {
AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
if (nodeInfo != null) {
List<AccessibilityNodeInfo> list = nodeInfo.findAccessibilityNodeInfosByViewId(id);
nodeInfo.recycle();
for (AccessibilityNodeInfo item : list) {
return item.getText() + "";
}
}
return "";
}
判断View是否存在
/**
* 判断页面是否有此文本的view
*/
private boolean viewByText(String str) {
AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
if (nodeInfo != null) {
List<AccessibilityNodeInfo> list = nodeInfo.findAccessibilityNodeInfosByText(str);
nodeInfo.recycle();
for (AccessibilityNodeInfo item : list) {
if (str.equals(item.getText().toString())) {
return true;
}
}
}
return false;
}
模拟滑动屏幕
//上划主页面
private void MainActivityToUp() {
final int x = ScreenUtils.getWindowsWidth(this) / 2;
final int y = ScreenUtils.getWindowsHeight(this) / 2;
Path path = new Path();
path.moveTo(x, y);
path.lineTo(x, 0);
GestureDescription.Builder builder = new GestureDescription.Builder();
builder.addStroke(new GestureDescription.StrokeDescription(path, 100L, 50L));
GestureDescription gesture = builder.build();
dispatchGesture(gesture, new GestureResultCallback() {
@Override
public void onCompleted(GestureDescription gestureDescription) {
super.onCompleted(gestureDescription);
Log.e(TAG, "onCompleted: 主页上滑.");
}
@Override
public void onCancelled(GestureDescription gestureDescription) {
super.onCancelled(gestureDescription);
}
}, null);
}
具体实现
1.获取我们需要操作的View的ID
dianzan.png
点赞按钮 id:com.ss.android.ugc.aweme:id/c99
用户名 id:com.ss.android.ugc.aweme:id/title
文本信息 id:com.ss.android.ugc.aweme:id/ag7
2.新建一个DouyinService服务类继承AccessibilityService,重写onAccessibilityEvent(AccessibilityEvent event),和onInterrupt(), 我们主要操作onAccessibilityEvent(AccessibilityEvent event).他返回了我们需要的事件信息.
3.在清单文件中注册服务,xml/accessibility是配置信息
<service
android:name=".service.DouyinService"
android:enabled="true"
android:exported="true"
android:label="抖音小助手2.0"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>
<meta-data
android:name="android.accessibilityservice"
android:resource="@xml/accessibility" />
</service>
之后就是在onAccessibilityEvent(AccessibilityEvent event)的逻辑处理
//显示更新提示框
if (event.getClassName().toString().equals("com.ss.android.ugc.aweme.update.j")) {
perforGlobalClick("com.ss.android.ugc.aweme:id/cev");//点击"以后再说"
}
//跳过广告
if (event.getClassName().toString().equals("com.ss.android.ugc.aweme.splash.SplashActivity")) {
perforGlobalClick("com.ss.android.ugc.aweme:id/ce2");//点击"跳过广告"
}
//点赞
if ("android.widget.ImageView".equals(event.getClassName().toString())) {
perforGlobalClick("com.ss.android.ugc.aweme:id/c99");
Log.i(TAG, "用户名:" + getTextById(" id:com.ss.android.ugc.aweme:id/title"));
Log.i(TAG, "文本信息:" + getTextById(" id:com.ss.android.ugc.aweme:id/ag7"));
}
运行项目
最后 在手机设置-->服务功能(或无障碍)-->抖音助手2.0 选择打开,然后开启抖音就可以了
sj.jpg
技术交流 QQ:390211696
网友评论