Android实现微信自动向附近的人打招呼(AccessibilityService)

Android实现微信自动向附近的人打招呼(AccessibilityService),第1张

概述学习功能强大的AccessibilityService!!!以下是本人根据自动抢红包的实现思路敲的用于微信自动向附近的人打招呼的核心代码

学习功能强大的AccessibilityService!!!
以下是本人根据自动抢红包的实现思路敲的用于微信自动向附近的人打招呼的核心代码

public class autoService extends AccessibilityService implements VIEw.OnClickListener { private static final String TAG = "test"; /** * 微信的包名 */ static final String WECHAT_PACKAGEname = "com.tencent.mm"; /** * 推送消息在通知栏的关键字,设置为推送账号名,如【十点读书】 */ static final String PUSH_TEXT_KEY = "十点读书"; /** * 推送链接的关键字,所有推送链接的标题都需要包含此关键字:如【深度好文】 */ private static final String URL_TEXT_KEY = "深度好文"; /** * 向附近的人自动打招呼的内容 */ private String hello = "测试APP自动打招呼功能,这是一条测试信息"; boolean startFunc2 = false;//标记是否开启自动添加附近的人为好友的功能; int i = 0;//记录已打招呼的人数 int page=1;//记录附近的人列表页码,初始页码为1 int prepos = -1;//记录页面跳转来源,0--从附近的人页面跳转到详细资料页,1--从打招呼页面跳转到详细资料页 @OverrIDe public voID onAccessibilityEvent(final AccessibilityEvent event) { int eventType = event.getEventType(); //通知栏事件 //自动打开推送的链接 if (eventType == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED) { List<CharSequence> texts = event.getText(); if (!texts.isEmpty()) { for (CharSequence t : texts) {  String text = String.valueOf(t);  if (text.contains(PUSH_TEXT_KEY)) {  openNotification(event);  openDelay(1000,URL_TEXT_KEY);  } } } } //自动加人 if (!startFunc2) { return; } if (eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && event.getClassname().equals("com.tencent.mm.ui.LauncherUI")) { //记录打招呼人数置零 i = 0; //当前在微信聊天页就点开发现 openNext("发现"); //然后跳转到附近的人 openDelay(1000,"附近的人"); } else if (event.getClassname().equals("com.tencent.mm.plugin.nearby.ui.NearbyFrIEndsUI") && eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) { prepos = 0; //当前在附近的人界面就点选人打招呼 AccessibilityNodeInfo nodeInfo = getRootInActiveWindow(); List<AccessibilityNodeInfo> List = nodeInfo.findAccessibilityNodeInfosByText("米以内"); Log.d("name","附近的人列表人数: " + List.size()); if (i < (List.size()*page) ){ List.get(i%List.size()).performAction(AccessibilityNodeInfo.ACTION_CliCK); List.get(i%List.size()).getParent().performAction(AccessibilityNodeInfo.ACTION_CliCK); } else if (i == List.size()*page) { //本页已全部打招呼,所以下滑列表加载下一页,每次下滑的距离是一屏 for (int i = 0; i < nodeInfo.getChild(0).getChildCount(); i++) {  if (nodeInfo.getChild(0).getChild(i).getClassname().equals("androID.Widget.ListVIEw")) {  AccessibilityNodeInfo node_lsv = nodeInfo.getChild(0).getChild(i);  node_lsv.performAction(AccessibilityNodeInfo.ACTION_SCRolL_FORWARD);  page++;  new Thread(new Runnable() {  @OverrIDe  public voID run() {  try {   Thread.sleep(1000);  } catch (InterruptedException mE) {   mE.printstacktrace();  }  AccessibilityNodeInfo nodeInfo_ = getRootInActiveWindow();  List<AccessibilityNodeInfo> List_ = nodeInfo_.findAccessibilityNodeInfosByText("米以内");  Log.d("name","列表人数: "+List_.size());  //滑动之后,上一页的最后一个item为当前的第一个item,所以从第二个开始打招呼  List_.get(1).performAction(AccessibilityNodeInfo.ACTION_CliCK);  List_.get(1).getParent().performAction(AccessibilityNodeInfo.ACTION_CliCK);  }  }).start();  } } } } else if (event.getClassname().equals("com.tencent.mm.plugin.profile.ui.ContactInfoUI") && eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) { if (prepos == 1) { //从打招呼界面跳转来的,则点击返回到附近的人页面 performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK); i++; } else if (prepos == 0) { //从附近的人跳转来的,则点击打招呼按钮 AccessibilityNodeInfo nodeInfo = getRootInActiveWindow(); if (nodeInfo == null) {  Log.w(TAG,"rootwindow为空");  return; } List<AccessibilityNodeInfo> List = nodeInfo.findAccessibilityNodeInfosByText("打招呼"); if (List.size() > 0) {  List.get(List.size() - 1).performAction(AccessibilityNodeInfo.ACTION_CliCK);  List.get(List.size() - 1).getParent().performAction(AccessibilityNodeInfo.ACTION_CliCK); } else {  //如果遇到已加为好友的则界面的“打招呼”变为“发消息",所以直接返回上一个界面并记录打招呼人数+1  performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);  i++; } } } else if (event.getClassname().equals("com.tencent.mm.ui.contact.SayHIEditUI") && eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) { //当前在打招呼页面 prepos = 1; //输入打招呼的内容并发送 inputHello(hello); openNext("发送"); } //自动从桌面打开微信,利用微信多开助手可实现多个微信账号之间的切换// if(topActivity.equals("com.huawei.androID.launcher.Launcher")){// openNext(event,"微信");// AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();// nodeInfo.getChildCount();// for (int i=0;i<nodeInfo.getChildCount();i++){// String name=nodeInfo.getChild(i).getVIEwIDResourcename();// }// } } /** * 打开通知栏消息 */ private voID openNotification(AccessibilityEvent event) { if (event.getParcelableData() == null || !(event.getParcelableData() instanceof Notification)) { return; } //以下是精华,将微信的通知栏消息打开 Notification notification = (Notification) event.getParcelableData(); PendingIntent pendingIntent = notification.contentIntent; try { pendingIntent.send(); } catch (PendingIntent.CanceledException e) { e.printstacktrace(); } } /** * 点击匹配的nodeInfo * @param str text关键字 */ private voID openNext(String str) { AccessibilityNodeInfo nodeInfo = getRootInActiveWindow(); if (nodeInfo == null) { Log.w(TAG,"rootwindow为空"); return; } List<AccessibilityNodeInfo> List = nodeInfo.findAccessibilityNodeInfosByText(str); Log.d("name","匹配个数: " + List.size()); if (List.size() > 0) { List.get(List.size() - 1).performAction(AccessibilityNodeInfo.ACTION_CliCK); List.get(List.size() - 1).getParent().performAction(AccessibilityNodeInfo.ACTION_CliCK); }// if ("com.tencent.mm.plugin.luckymoney.ui.LuckyMoneyReceiveUI".equals(event.getClassname())) {// //点中了红包,下一步就是去拆红包// checkKey1();// } else if ("com.tencent.mm.plugin.luckymoney.ui.LuckyMoneyDetailUI".equals(event.getClassname())) {// //拆完红包后看详细的纪录界面// } else if ("com.tencent.mm.ui.LauncherUI".equals(event.getClassname())) {// //在聊天界面,去点中红包// checkKey2();// } } //延迟打开界面 private voID openDelay(final int delaytime,final String text) { new Thread(new Runnable() { @OverrIDe public voID run() { try {  Thread.sleep(delaytime); } catch (InterruptedException mE) {  mE.printstacktrace(); } openNext(text); } }).start(); } //自动输入打招呼内容 private voID inputHello(String hello) { AccessibilityNodeInfo nodeInfo = getRootInActiveWindow(); //找到当前获取焦点的vIEw AccessibilityNodeInfo target = nodeInfo.findFocus(AccessibilityNodeInfo.FOCUS_input); if (target == null) { Log.d(TAG,"inputHello: null"); return; } ClipboardManager clipboard = (ClipboardManager) getSystemService(CliPBOARD_SERVICE); ClipData clip = ClipData.newPlainText("label",hello); clipboard.setPrimaryClip(clip); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { target.performAction(AccessibilityNodeInfo.ACTION_PASTE); } } @OverrIDe public voID onInterrupt() { Toast.makeText(this,"服务已中断",Toast.LENGTH_SHORT).show(); } @OverrIDe protected voID onServiceConnected() { super.onServiceConnected(); Toast.makeText(this,"连接服务",Toast.LENGTH_SHORT).show(); } @OverrIDe public voID onCreate() { super.onCreate(); createfloatVIEw(); } WindowManager wm; button floatbtn; //创建悬浮按钮 private voID createfloatVIEw() { WindowManager.LayoutParams pl = new WindowManager.LayoutParams(); wm = (WindowManager) getSystemService(getApplication().WINDOW_SERVICE); pl.type = WindowManager.LayoutParams.TYPE_PHONE; pl.format = PixelFormat.RGBA_8888; pl.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; pl.gravity = Gravity.RIGHT | Gravity.BottOM; pl.x = 0; pl.y = 0; pl.wIDth = 200; pl.height = 200; LayoutInflater inflater = LayoutInflater.from(this); floatbtn = (button) inflater.inflate(R.layout.floatbtn,null); wm.addVIEw(floatbtn,pl); floatbtn.setonClickListener(this); } @OverrIDe public voID onClick(VIEw v) { if (startFunc2) { floatbtn.setText("启用加人"); } else { floatbtn.setText("停止加人"); } startFunc2 = !startFunc2; }}

启用这个服务的代码:

//打开系统设置中辅助功能Intent intent = new Intent(androID.provIDer.Settings.ACTION_ACCESSIBIliTY_SETTINGS);startActivity(intent);

添加权限:

<uses-permission androID:name="androID.permission.BIND_ACCESSIBIliTY_SERVICE" />

demo地址:点击打开链接

本文已被整理到了《Android微信开发教程汇总》,《@L_502_2@》欢迎大家学习阅读。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的Android实现微信自动向附近的人打招呼(AccessibilityService)全部内容,希望文章能够帮你解决Android实现微信自动向附近的人打招呼(AccessibilityService)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1147395.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存