編輯:關於Android編程
學習功能強大的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地址:點擊打開鏈接
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。
Android系統裡面有個東西很好用,也很常用,那就是Toast,但是長期使用也會發現,Toast有他的不足之處:形式單一,只有文字,風格不變等等,那麼要如
Windows Manager是一款窗口管理終端,可以遠程連接到Linux的X桌面進行管理,與服務器端產生一個session相互通信。最近在網上看見一個人在烏
ATCID主要用來處理PC端傳輸過來的AT命令,從AT命令實際處理的地方來說,主要分為3類: 1. 需要Modem來處理的AT命令; 2. 需要在nativ
ImageView的Scaletype決定了圖片在View上顯示時的樣子,如進行何種比例的縮放,及顯示圖片的整體還是部分,等等。設置的方式包括:1. 在layout xm