欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2024-02-26 18:19:04
学习功能强大的accessibilityservice!!! 以下是本人根据自动抢红包的实现思路敲的用于微信自动向附近的人打招呼的核心代码 public c...

学习功能强大的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微信开发教程汇总》,《java微信开发教程汇总》欢迎大家学习阅读。

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