Android實現微信自動向附近的人打招呼(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; //當前在微信聊天頁就點開發(fā)現 openNext("發(fā)現"); //然后跳轉到附近的人 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 { //如果遇到已加為好友的則界面的“打招呼”變?yōu)椤鞍l(fā)消息",所以直接返回上一個界面并記錄打招呼人數+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; //輸入打招呼的內容并發(fā)送 inputHello(hello); openNext("發(fā)送"); } //自動從桌面打開微信,利用微信多開助手可實現多個微信賬號之間的切換 // 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; //創(chuàng)建懸浮按鈕 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; } }
啟用這個服務的代碼:
//打開系統(tǒng)設置中輔助功能 Intent intent = new Intent(android.provider.Settings.ACTION_ACCESSIBILITY_SETTINGS); startActivity(intent);
添加權限:
<uses-permission android:name="android.permission.BIND_ACCESSIBILITY_SERVICE" />
demo地址:點擊打開鏈接
本文已被整理到了《Android微信開發(fā)教程匯總》,《java微信開發(fā)教程匯總》歡迎大家學習閱讀。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
android開發(fā)教程之使用線程實現視圖平滑滾動示例
這篇文章主要介紹了android使用線程實現視圖平滑滾動示例,需要的朋友可以參考下2014-03-03Android新特性頁面之ViewPager拖拽到最后一頁再拖拽打開其他Activity(三種方法)
這篇文章主要介紹了Android新特性頁面之ViewPager拖拽到最后一頁再拖拽打開其他Activity的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-08-08詳解基于Android的Appium+Python自動化腳本編寫
這篇文章主要介紹了詳解基于Android的Appium+Python自動化腳本編寫,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-08-08Kotlin中的惰性操作容器Sequence序列使用原理詳解
這篇文章主要為大家介紹了Kotlin中的惰性操作容器Sequence序列使用原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09Android中Android Virtual Device(AVD)使用教程
這篇文章主要介紹了Android中Android Virtual Device(AVD)使用教程,本文還對使用過程中發(fā)生的一些錯誤給出了處理方法,需要的朋友可以參考下2015-01-01