Android实现微信自动向附近的人打招呼(AccessibilityService)
学习功能强大的AccessibilityService!!!
以下是本人根据自动抢红包的实现思路敲的用于微信自动向附近的人打招呼的核心代码
publicclassAutoServiceextendsAccessibilityServiceimplementsView.OnClickListener{ privatestaticfinalStringTAG="test"; /** *微信的包名 */ staticfinalStringWECHAT_PACKAGENAME="com.tencent.mm"; /** *推送消息在通知栏的关键字,设置为推送账号名,如【十点读书】 */ staticfinalStringPUSH_TEXT_KEY="十点读书"; /** *推送链接的关键字,所有推送链接的标题都需要包含此关键字:如【深度好文】 */ privatestaticfinalStringURL_TEXT_KEY="深度好文"; /** *向附近的人自动打招呼的内容 */ privateStringhello="测试APP自动打招呼功能,这是一条测试信息"; booleanstartFunc2=false;//标记是否开启自动添加附近的人为好友的功能; inti=0;//记录已打招呼的人数 intpage=1;//记录附近的人列表页码,初始页码为1 intprepos=-1;//记录页面跳转来源,0--从附近的人页面跳转到详细资料页,1--从打招呼页面跳转到详细资料页 @Override publicvoidonAccessibilityEvent(finalAccessibilityEventevent){ inteventType=event.getEventType(); //通知栏事件 //自动打开推送的链接 if(eventType==AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED){ List<CharSequence>texts=event.getText(); if(!texts.isEmpty()){ for(CharSequencet:texts){ Stringtext=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,"附近的人"); }elseif(event.getClassName().equals("com.tencent.mm.plugin.nearby.ui.NearbyFriendsUI")&&eventType==AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED){ prepos=0; //当前在附近的人界面就点选人打招呼 AccessibilityNodeInfonodeInfo=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); }elseif(i==list.size()*page){ //本页已全部打招呼,所以下滑列表加载下一页,每次下滑的距离是一屏 for(inti=0;i<nodeInfo.getChild(0).getChildCount();i++){ if(nodeInfo.getChild(0).getChild(i).getClassName().equals("android.widget.ListView")){ AccessibilityNodeInfonode_lsv=nodeInfo.getChild(0).getChild(i); node_lsv.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD); page++; newThread(newRunnable(){ @Override publicvoidrun(){ try{ Thread.sleep(1000); }catch(InterruptedExceptionmE){ mE.printStackTrace(); } AccessibilityNodeInfonodeInfo_=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(); } } } }elseif(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++; }elseif(prepos==0){ //从附近的人跳转来的,则点击打招呼按钮 AccessibilityNodeInfonodeInfo=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++; } } }elseif(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,"微信"); //AccessibilityNodeInfonodeInfo=getRootInActiveWindow(); //nodeInfo.getChildCount(); //for(inti=0;i<nodeInfo.getChildCount();i++){ //Stringname=nodeInfo.getChild(i).getViewIdResourceName(); //} //} } /** *打开通知栏消息 */ privatevoidopenNotification(AccessibilityEventevent){ if(event.getParcelableData()==null||!(event.getParcelableData()instanceofNotification)){ return; } //以下是精华,将微信的通知栏消息打开 Notificationnotification=(Notification)event.getParcelableData(); PendingIntentpendingIntent=notification.contentIntent; try{ pendingIntent.send(); }catch(PendingIntent.CanceledExceptione){ e.printStackTrace(); } } /** *点击匹配的nodeInfo *@paramstrtext关键字 */ privatevoidopenNext(Stringstr){ AccessibilityNodeInfonodeInfo=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(); //}elseif("com.tencent.mm.plugin.luckymoney.ui.LuckyMoneyDetailUI".equals(event.getClassName())){ ////拆完红包后看详细的纪录界面 //}elseif("com.tencent.mm.ui.LauncherUI".equals(event.getClassName())){ ////在聊天界面,去点中红包 //checkKey2(); //} } //延迟打开界面 privatevoidopenDelay(finalintdelaytime,finalStringtext){ newThread(newRunnable(){ @Override publicvoidrun(){ try{ Thread.sleep(delaytime); }catch(InterruptedExceptionmE){ mE.printStackTrace(); } openNext(text); } }).start(); } //自动输入打招呼内容 privatevoidinputHello(Stringhello){ AccessibilityNodeInfonodeInfo=getRootInActiveWindow(); //找到当前获取焦点的view AccessibilityNodeInfotarget=nodeInfo.findFocus(AccessibilityNodeInfo.FOCUS_INPUT); if(target==null){ Log.d(TAG,"inputHello:null"); return; } ClipboardManagerclipboard=(ClipboardManager)getSystemService(CLIPBOARD_SERVICE); ClipDataclip=ClipData.newPlainText("label",hello); clipboard.setPrimaryClip(clip); if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN_MR2){ target.performAction(AccessibilityNodeInfo.ACTION_PASTE); } } @Override publicvoidonInterrupt(){ Toast.makeText(this,"服务已中断",Toast.LENGTH_SHORT).show(); } @Override protectedvoidonServiceConnected(){ super.onServiceConnected(); Toast.makeText(this,"连接服务",Toast.LENGTH_SHORT).show(); } @Override publicvoidonCreate(){ super.onCreate(); createFloatView(); } WindowManagerwm; Buttonfloatbtn; //创建悬浮按钮 privatevoidcreateFloatView(){ WindowManager.LayoutParamspl=newWindowManager.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; LayoutInflaterinflater=LayoutInflater.from(this); floatbtn=(Button)inflater.inflate(R.layout.floatbtn,null); wm.addView(floatbtn,pl); floatbtn.setOnClickListener(this); } @Override publicvoidonClick(Viewv){ if(startFunc2){ floatbtn.setText("启用加人"); }else{ floatbtn.setText("停止加人"); } startFunc2=!startFunc2; } }
启用这个服务的代码:
//打开系统设置中辅助功能 Intentintent=newIntent(android.provider.Settings.ACTION_ACCESSIBILITY_SETTINGS); startActivity(intent);
添加权限:
<uses-permissionandroid:name="android.permission.BIND_ACCESSIBILITY_SERVICE"/>
demo地址:点击打开链接
本文已被整理到了《Android微信开发教程汇总》,《java微信开发教程汇总》欢迎大家学习阅读。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。