最近在研究Android辅助服务,实现了这个小工具,也算是对最近学习的一个总结。
通过Android 无障碍辅助功能实现模拟点击控件来实现
检查被删好友有两种方法:
本文采用建群的方式进行检查。
本人微信有300好友,全部检测一遍只需3分钟即可,亲测已经成功,
但是建群没有超过40人 会有个别好友会受到打扰消息,可能是微信哪里的bug,具体原因未知。
此软件通过无障碍辅助进行模拟点击,无任何外挂木马,无封号风险
/**
* Created by wanglj on 16/10/20.
*/
public class InspectWechatFriendService extends AccessibilityService{
@Override
protected void onServiceConnected() {//辅助服务被打开后 执行此方法
super.onServiceConnected();
}
@Override
public void onAccessibilityEvent(AccessibilityEvent accessibilityEvent) {//监听手机当前窗口状态改变 比如 Activity 跳转,内容变化,按钮点击等事件
}
@Override
public void onInterrupt() {//辅助服务被关闭 执行此方法
}
}
通过getRootInActiveWindow方法获取当前窗口信息,通过findAccessibilityNodeInfosByText方法找到当前对应控件进行模拟点击
public class PerformClickUtils {
/**
* 在当前页面查找文字内容并点击
*
* @param text
*/
public static void findTextAndClick(AccessibilityService accessibilityService,String text) {
AccessibilityNodeInfo accessibilityNodeInfo = accessibilityService.getRootInActiveWindow();
if (accessibilityNodeInfo == null) {
return;
}
List nodeInfoList = accessibilityNodeInfo.findAccessibilityNodeInfosByText(text);
if (nodeInfoList != null && !nodeInfoList.isEmpty()) {
for (AccessibilityNodeInfo nodeInfo : nodeInfoList) {
if (nodeInfo != null && (text.equals(nodeInfo.getText()) || text.equals(nodeInfo.getContentDescription()))) {
performClick(nodeInfo);
break;
}
}
}
}
/**
* 检查viewId进行点击
*
* @param accessibilityService
* @param id
*/
public static void findViewIdAndClick(AccessibilityService accessibilityService,String id) {
AccessibilityNodeInfo accessibilityNodeInfo = accessibilityService.getRootInActiveWindow();
if (accessibilityNodeInfo == null) {
return;
}
List nodeInfoList = accessibilityNodeInfo.findAccessibilityNodeInfosByViewId(id);
if (nodeInfoList != null && !nodeInfoList.isEmpty()) {
for (AccessibilityNodeInfo nodeInfo : nodeInfoList) {
if (nodeInfo != null) {
performClick(nodeInfo);
break;
}
}
}
}
/**
* 在当前页面查找对话框文字内容并点击
*
* @param text1 默认点击text1
* @param text2
*/
public static void findDialogAndClick(AccessibilityService accessibilityService,String text1, String text2) {
AccessibilityNodeInfo accessibilityNodeInfo = accessibilityService.getRootInActiveWindow();
if (accessibilityNodeInfo == null) {
return;
}
List dialogWait = accessibilityNodeInfo.findAccessibilityNodeInfosByText(text1);
List dialogCOnfirm= accessibilityNodeInfo.findAccessibilityNodeInfosByText(text2);
if (!dialogWait.isEmpty() && !dialogConfirm.isEmpty()) {
for (AccessibilityNodeInfo nodeInfo : dialogWait) {
if (nodeInfo != null && text1.equals(nodeInfo.getText())) {
performClick(nodeInfo);
break;
}
}
}
}
//模拟点击事件
public static void performClick(AccessibilityNodeInfo nodeInfo) {
if (nodeInfo == null) {
return;
}
if (nodeInfo.isClickable()) {
nodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
} else {
performClick(nodeInfo.getParent());
}
}
//模拟返回事件
public static void performBack(AccessibilityService service) {
if (service == null) {
return;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
service.performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);
}
}
}
获取当前窗口的classname 通过classname进行判断当前手机处于某个界面
下面代码逻辑:
下面为对应逻辑代码:
@Override
public void onAccessibilityEvent(AccessibilityEvent accessibilityEvent) {//监听手机当前窗口状态改变 比如 Activity 跳转,内容变化,按钮点击等事件
//如果手机当前界面的窗口发送变化
if (accessibilityEvent.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
//获取当前activity的类名:
String currentWindowActivity = accessibilityEvent.getClassName().toString();
if(!hasComplete){
if ("com.tencent.mm.ui.contact.SelectContactUI".equals(currentWindowActivity)) {
canChecked = true;
createGroup();
} else if ("com.tencent.mm.ui.chatting.ChattingUI".equals(currentWindowActivity)) {
getDeleteFriend();
} else if ("com.tencent.mm.plugin.chatroom.ui.ChatroomInfoUI".equals(currentWindowActivity)) {
deleteGroup();
}else if("com.tencent.mm.ui.LauncherUI".equals(currentWindowActivity)){
PerformClickUtils.findTextAndClick(this,"更多功能按钮");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
PerformClickUtils.findTextAndClick(this,"发起群聊");
}
}else{
nickNameList.clear();
deleteList.clear();
sortItems.clear();
startActivity(new Intent(this, DeleteFriendListActivity.class));
}
}
}
/**
* 模拟创建群组步骤
*/
private void createGroup() {
AccessibilityNodeInfo accessibilityNodeInfo = getRootInActiveWindow();
if (accessibilityNodeInfo == null) {
return;
}
List listview = accessibilityNodeInfo.findAccessibilityNodeInfosByViewId(selectUI_listview_id);
int count = 0;
if (!listview.isEmpty()) {
while (canChecked) {
List checkboxList = accessibilityNodeInfo.findAccessibilityNodeInfosByViewId(selectUI_checkbox_id);
List sortList = accessibilityNodeInfo.findAccessibilityNodeInfosByViewId(selectUI_sortitem_id);
for(AccessibilityNodeInfo nodeInfo:sortList){
if(nodeInfo != null && nodeInfo.getText()!= null){
sortItems.add(nodeInfo.getText().toString());
}
}
for (AccessibilityNodeInfo nodeInfo : checkboxList) {
String nickname = nodeInfo.getParent().findAccessibilityNodeInfosByViewId(selectUI_nickname_id).get(0).getText().toString();
Log.e(TAG, "nickname = " + nickname);
if (!nickNameList.contains(nickname)) {
nickNameList.add(nickname);
performClick(nodeInfo);
count++;
if (count >= GROUP_COUNT || nickNameList.size() >= listview.get(0).getCollectionInfo().getRowCount() - sortItems.size() - 2) {
List createButtOns= accessibilityNodeInfo.findAccessibilityNodeInfosByViewId(selectUI_create_button_id);
if (!createButtons.isEmpty()) {
performClick(createButtons.get(0));
}
if(nickNameList.size() >= listview.get(0).getCollectionInfo().getRowCount() - sortItems.size() - 2){
hasComplete = true;
}
return;
}
}
}
listview.get(0).performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/**
* 模拟获取被删好友列表步骤
*/
private void getDeleteFriend() {
AccessibilityNodeInfo accessibilityNodeInfo = getRootInActiveWindow();
if (accessibilityNodeInfo == null) {
return;
}
List nodeInfoList = accessibilityNodeInfo.findAccessibilityNodeInfosByViewId(chattingUI_message_id);
for (AccessibilityNodeInfo nodeInfo : nodeInfoList) {
if (nodeInfo != null && nodeInfo.getText() != null && nodeInfo.getText().toString().contains("你无法邀请未添加你为好友的用户进去群聊,请先向")) {
String str = nodeInfo.getText().toString();
str = str.replace("你无法邀请未添加你为好友的用户进去群聊,请先向", "");
str = str.replace("发送朋友验证申请。对方通过验证后,才能加入群聊。", "");
String[] arr = str.split("、");
deleteList.addAll(Arrays.asList(arr));
Preferences.saveDeleteFriends(this);
Log.e(TAG, "deleteList.size():" + deleteList.size());
Toast.makeText(this, "僵尸粉数量:" + deleteList.size(), Toast.LENGTH_SHORT).show();
break;
}
}
PerformClickUtils.findTextAndClick(this,"聊天信息");
}
/**
* 退出群组步骤
*/
private void deleteGroup(){
AccessibilityNodeInfo accessibilityNodeInfo = getRootInActiveWindow();
if (accessibilityNodeInfo == null) {
return;
}
List nodeInfoList = accessibilityNodeInfo.findAccessibilityNodeInfosByViewId(groupinfoUI_listview_id);
if (!nodeInfoList.isEmpty()) {
nodeInfoList.get(0).performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
nodeInfoList.get(0).performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
PerformClickUtils.findTextAndClick(this,"删除并退出");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
PerformClickUtils.findTextAndClick(this,"删除并退出");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// if(Utils.getVersion(this).equals(WECHAT_VERSION_27)){
// PerformClickUtils.findTextAndClick(this,"确定");
// }else{
PerformClickUtils.findTextAndClick(this,"离开群聊");
PerformClickUtils.findTextAndClick(this,"确定");
// }
}
}
uiautomatorviewer可以检查当前手机的布局结构,如果想更精确的找到控件位置,uiautomatorviewer必不可少!
使用方法:
$uiautomatorviewer
整体效果图:
(ps: 文章来源于网络,仅供学习与交流,如有侵权,请联系我们删除。)