发布于 2017-01-06 09:34:43 | 936 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


这篇文章主要为大家详细介绍了实现微信自动向附近的人打招呼,实现收到指定账户推送文章时自动进入微信打开链接,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

学习功能强大的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地址:点击打开链接

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



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务