微信打招呼
学习功能强大的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" />
相关阅读
社群微信小程序开发以微信为切入口,通过为用户提供便捷服务而让用户有好的体验。微信小程序自面世以来便受到不少企业与用户的关注
谈到微信朋友圈营销,好像微商遭遇滑铁卢,去年人人微商,今年朋友圈冷清了很多,不过有效做好微信朋友圈,还是商机无限的。我有个朋友家中
Windows自动弹出MSN中文网界面解决办法 原因:每当用户连接到网络时,Windows 会向微软的一个域名发送访问请求,访问结果作为网络连接
1)微信: 零钱:一天最多转100次,单日单笔最高20W,无月限额 银行卡:受银行卡办理时的设置限制(超出时,不能更改,只能换卡)2)支付宝:
随着用户和开发者的信息推送诉求日益增长,10月12日,微信官方消息显示,微信小程序模板消息进行优化调整,将支持一次性和长期性订阅消息