这里写目录标题
1、创建Service 集成AccessibilityService
Service 的onAccessibilityEvent 中监听系统变化,过滤自己感兴趣的消息事件、没什么好说的,直接开始代码
public class AutoHelperServer extends AccessibilityService {
public static final String TAG = "zyb";
public static final String wechatPackageName = "com.tencent.mm";
private boolean startDelay;
private long lastExecTimeStamp;
@Override
protected void onServiceConnected() {
super.onServiceConnected();
Log.i(TAG, "====建立服务链接====");
}
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
int eventType = event.getEventType();
Log.d(TAG, "eventType: " + eventType + "\t PackageName:" + event.getPackageName());
Log.d(TAG, "&& event.getSource(): " + event.getSource());
if ((eventType == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED || eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) && event.getSource() != null && event.getPackageName() != null) {
CharSequence packageName = event.getPackageName();
Log.d(TAG, "packageName=: " + packageName);
if (!wechatPackageName.equals(packageName)) {
return;
}
if (this.startDelay && System.currentTimeMillis() - this.lastExecTimeStamp < 300L) {
Log.d(TAG, "已经接听了没有比较再接听一次了 : " + packageName);
return;
}
this.lastExecTimeStamp = System.currentTimeMillis();
this.startDelay = false;
CharSequence className = event.getClassName();
if (className != null) {
Log.d(TAG, "页面->: " + className);
}
autoConnectWeChatCall(event);
} else if (eventType == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED && event.getPackageName() != null) {
Log.d(TAG, "通知栏发生变化了...");
Log.d(TAG, "eventType: " + eventType + "\t PackageName:" + event.getPackageName());
CharSequence className = event.getClassName();
if (className != null) {
Log.d(TAG, "页面->: " + className);
}
if (event.getParcelableData() != null && event.getParcelableData() instanceof Notification) {
if ("com.tencent.mm".equals(event.getPackageName())) {
Notification notification = (Notification) event.getParcelableData();
PendingIntent pendingIntent1 = notification.contentIntent;
Log.d(TAG, "pendingIntent..." + pendingIntent1);
try {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
String title = notification.extras.getString(Notification.EXTRA_TITLE);
String content = notification.extras.getString(Notification.EXTRA_TEXT);
Log.d(TAG, "title..." + title + "\tcontent :" + content);
} else {
Log.d(TAG, "版本比较低..." + pendingIntent1);
}
if (pendingIntent1 != null) {
pendingIntent1.send();
}
} catch (PendingIntent.CanceledException e) {
e.printStackTrace();
}
}
}
}
}
private void autoConnectWeChatCall(AccessibilityEvent event) {
AccessibilityNodeInfo source = event.getSource();
int type = 1;
Log.d(TAG, "event.getClassName()..." + event.getClassName() + "\t");
if (source != null) {
//微信聊天页面,是一个listView,寻找红包记录
List<AccessibilityNodeInfo> listAccessibilityNodes = source.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/bp0");
if (listAccessibilityNodes != null) {
Log.d(TAG, "是否获取到聊天的list列表数据-> " + listAccessibilityNodes.size());
if (listAccessibilityNodes.size() >= 1) {
Log.d(TAG, " hBaccessibilityNodeInfos.get(0).getChildCount()::" + listAccessibilityNodes.get(0).getChildCount());
//获取到recyclerView节点数据
AccessibilityNodeInfo accessibilityNodeInfo = listAccessibilityNodes.get(0); //列表
//检索聊天记录上所有的红包记录
List<AccessibilityNodeInfo> list = accessibilityNodeInfo.findAccessibilityNodeInfosByText("微信红包");
Log.d(TAG, "总共有几个红包 :" + list.size());
//默认点击最后一个红包
tap(this, list.get(list.size() - 1));
}
}
//开始拆红包页面
List<AccessibilityNodeInfo> accessibilityNodeInfosByViewId = source.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/j6h");
if (accessibilityNodeInfosByViewId != null) {
if (accessibilityNodeInfosByViewId.size() > 0) {
if (AppGlobalVariable.INSTANCE.getAutoWechatPackMessages()) {
}
//红包开的按钮
AccessibilityNodeInfo accessibilityNodeInfo = accessibilityNodeInfosByViewId.get(0);
tap(this, accessibilityNodeInfo);
Log.d(TAG, "开始点击红包了:");
}
}
//微信通知栏上拒绝、接听页面
List<AccessibilityNodeInfo> accessibilityNodeInfos = source.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/kg6");
Log.d(TAG, "autoConnectWeChatCall..." + accessibilityNodeInfos.size() + "\t ");
if (accessibilityNodeInfos.size() >= 1 && accessibilityNodeInfos.get(0).getChildCount() == 0) {
String text = accessibilityNodeInfos.get(0).getText().toString();
Log.d(TAG, "autoConnectWeChatCall... text :" + text);
if (text.equals("邀请你视频通话...")) {
if (AppGlobalVariable.INSTANCE.getAutoWechatAudioMessages()) {
tap(this, accessibilityNodeInfos.get(0));
Log.d(TAG, "邀请你视频通话...");
}
} else if (text.equals("邀请你语音通话...")) {
if (AppGlobalVariable.INSTANCE.getAutoWechatAudioMessages()) {
tap(this, accessibilityNodeInfos.get(0));
Log.d(TAG, "邀请你语音通话...");
}
}
}
}
if (event.getClassName() == null && this.getRootInActiveWindow() != null) {
Log.d(TAG, "微信窗口自动变化");
List<AccessibilityNodeInfo> crdList = this.getRootInActiveWindow().findAccessibilityNodeInfosByViewId("com.tencent.mm:id/crd");
if ((crdList != null) && !crdList.isEmpty()) {
AccessibilityNodeInfo var9 = crdList.get(0); //crd
if (var9 != null) {
var9 = var9.getChild(0); //frameLayout
if (var9 != null) {
var9 = var9.getChild(0); //frameLayout
if (var9 != null) {
var9 = var9.getChild(0); //View
if (var9 != null) {
var9 = var9.getChild(0); //View
if (var9 != null) {
var9 = var9.getChild(0); //View
if (var9 != null) {
final AccessibilityNodeInfo var4 = var9.getChild(0); //View 最终接听电话的页面布局
if (var4 != null && var4.getChildCount() >= 7) {
CharSequence var13 = var4.getChild(0).getContentDescription();
if (var13 == null) {
var13 = (CharSequence) "";
} else {
Log.d(TAG, "getChild(0).contentDescription...");
}
String var13trim = var13.toString().trim();
Log.d(TAG, "var13trim" + var13trim);
String[] result = var13trim.split(" ");
Log.d(TAG, "result length" + result.length);
if (result.length >= 2) {
String wechatName = result[0];
String wechatType = result[1];
Log.d(TAG, "wechatName" + wechatName + "\t wechatType" + wechatType);
if (!wechatType.contains("视频通话")) {
if (wechatType.contains("语音通话")) {
type = 0;
} else {
type = -1;
}
}
if (type == -1) {
Log.d(TAG, "wechatType 是-1 ");
} else {
Log.d(TAG, "接听微信电话和视频 ");
if (type == 0) {
if (AppGlobalVariable.INSTANCE.getAutoWechatAudioMessages()) {
tapConnectBtnOrSpreadBtn(var4, this);
}
} else {
if (AppGlobalVariable.INSTANCE.getAutoWechatVideoMessages()) {
tapConnectBtnOrSpreadBtn(var4, this);
}
}
}
}
}
}
}
}
}
}
}
}
}
}
private void tapConnectBtnOrSpreadBtn(AccessibilityNodeInfo var4, final AutoHelperServer AutoHelperServer) {
final AccessibilityNodeInfo child = var4.getChild(var4.getChildCount() - 1);
if (child != null) {
CharSequence contentDescription = child.getContentDescription();
String contentDescriptionTrim = "";
if (contentDescription != null) {
contentDescriptionTrim = contentDescription.toString().trim();
Log.i(TAG, "contentDescriptionTrim:::" + contentDescriptionTrim);
}
if ("接听".equals(contentDescriptionTrim)) {
Log.i(TAG, "接听:::");
tap(AutoHelperServer, child);
this.startDelay = true;
} else if ("扬声器已关".equals(contentDescriptionTrim)) {
Log.i(TAG, "开启扬声器:::");
tap(AutoHelperServer, child);
this.startDelay = true;
}
}
}
@Override
public void onInterrupt() {
Log.i(TAG, "====onInterrupt====");
}
//=======================================================
public static final void tap(AccessibilityService var0, AccessibilityNodeInfo var1) {
if (var1 != null) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
Log.i(TAG, "开始 tap....." + Thread.currentThread().getName());
Rect rect = new Rect();
var1.getBoundsInScreen(rect);
GestureDescription.Builder gestureBuilder = new GestureDescription.Builder();
Path path = new Path();
Log.i(TAG, "rect.centerX()::" + rect.centerX() + "\t rect.centerY()::" + rect.centerY());
Log.i(TAG, "rect.left::" + rect.left + "\t rect.right()::" + rect.right + "\t rect.top()::" + rect.top + "\t rect.bottom()::" + rect.bottom);
path.moveTo(rect.centerX(), rect.centerY());
gestureBuilder.addStroke(new GestureDescription.StrokeDescription(path, 0L, 200L));
GestureDescription gestureDescription = gestureBuilder.build();
var0.dispatchGesture(gestureDescription, new GestureResultCallback() {
@Override
public void onCompleted(GestureDescription gestureDescription) {
super.onCompleted(gestureDescription);
Log.i(TAG, "onCompleted::");
}
@Override
public void onCancelled(GestureDescription gestureDescription) {
super.onCancelled(gestureDescription);
Log.i(TAG, "onCancelled::");
}
}, null);
} else {
Log.i(TAG, "收拾只能在7.0以上使用了");
}
} else {
Log.i(TAG, "var1 是空的.....");
}
}
}
2、注册无障碍服务
<service
android:name=".service.AutoHelperServer"
android:enabled="true"
android:exported="false"
android:label="@string/app_name"
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/auto_control_service_config" />
</service>
3、注册 auto_control_service_config配置文件
里边属性配置很重要
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeAllMask"
android:accessibilityFeedbackType="feedbackAllMask"
android:accessibilityFlags="flagDefault|flagIncludeNotImportantViews|flagReportViewIds|flagRetrieveInteractiveWindows|flagRequestFingerprintGestures"
android:canPerformGestures="true"
android:canRetrieveWindowContent="true"
android:canRequestFilterKeyEvents="true"
android:canRequestEnhancedWebAccessibility="true"
android:description="@string/description"
android:notificationTimeout="50"/>
4、调用前需要开启无障碍服务
public boolean isAccessibilitySettingsOn(Context context) {
int accessibilityEnabled = 0;
try {
accessibilityEnabled = Settings.Secure.getInt(context.getContentResolver(),
Settings.Secure.ACCESSIBILITY_ENABLED);
} catch (Settings.SettingNotFoundException e) {
Log.i("URL", "错误信息为:" + e.getMessage());
}
if (accessibilityEnabled == 1) {
String services = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
if (services != null) {
return services.toLowerCase().contains(context.getPackageName().toLowerCase());
}
}
return false;
}
boolean isAccessibilityOn=isAccessibilitySettingsOn(WechatActivity.this);
if (isAccessibilityOn){
Toast.makeText(WechatActivity.this,"辅助功能已经开启",Toast.LENGTH_SHORT).show();
}else {
Intent accessibleIntent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
startActivity(accessibleIntent);
}