Android實現微信自動向附近的人打招呼(AccessibilityService)_Android

來源:互聯網
上載者:User

學習功能強大的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地址:點擊開啟連結

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.