From c7530f1a595883f815b9689dbd3a8159e8d6bd30 Mon Sep 17 00:00:00 2001 From: Runt <qingingrunt2010@qq.com> Date: Sat, 31 Dec 2022 01:14:21 +0000 Subject: [PATCH] UI线程阻塞问题修复 --- app/src/main/java/com/auto/lyric/service/AutoInputService.java | 66 ++++++++++++++++++-------------- 1 files changed, 37 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/com/auto/lyric/service/AutoInputService.java b/app/src/main/java/com/auto/lyric/service/AutoInputService.java index c68f2c6..ca83317 100644 --- a/app/src/main/java/com/auto/lyric/service/AutoInputService.java +++ b/app/src/main/java/com/auto/lyric/service/AutoInputService.java @@ -23,7 +23,8 @@ public class AutoInputService extends AccessibilityService { final String TAG = "AutoInputService"; public static final int ACTION_SEND = 1001,//发送 - ACTION_PASTE = 1002;//粘贴 + ACTION_PASTE = 1002,//粘贴 + ACTION_START = 1000;//开始 BroadcastReceiver receiver = new BroadcastReceiver() { @Override @@ -57,9 +58,12 @@ Toast.makeText(getBaseContext(),"未找到控件",Toast.LENGTH_SHORT).show(); } }catch (Exception e){ - e.printStackTrace(); + //e.printStackTrace(); } + break; + case ACTION_START: + clickEdit(); break; } } @@ -70,25 +74,25 @@ @Override public void onAccessibilityEvent(AccessibilityEvent event) { int eventType = event.getEventType(); - Log.d(TAG," text:" + event.getText()+" class:"+event.getClassName() +" action:" + event.getAction() +" windowId:"+ event.getWindowId()); - Log.d(TAG,"event:"+event ); - Log.d(TAG,"ParcelableData:"+event.getParcelableData() ); + //Log.d(TAG," text:" + event.getText()+" class:"+event.getClassName() +" action:" + event.getAction() +" windowId:"+ event.getWindowId()); + //Log.d(TAG,"event:"+event ); + //Log.d(TAG,"ParcelableData:"+event.getParcelableData() ); AccessibilityNodeInfo nodeInfo = getRootInActiveWindow(); AccessibilityNodeInfo source = event.getSource();//当前界面的可访问节点信息 if(source != null){ //printView(source,"source == "); } - Log.d(TAG,"source:"+(source == null? null : source.getClassName()) ); - Log.d(TAG,"nodeInfo:"+ (nodeInfo == null? null : nodeInfo.getClassName()) ); + //Log.d(TAG,"source:"+(source == null? null : source.getClassName()) ); + //Log.d(TAG,"nodeInfo:"+ (nodeInfo == null? null : nodeInfo.getClassName()) ); switch (eventType) { case AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED://界面变化事件 - Log.e(TAG, "TYPE_WINDOW_CONTENT_CHANGED " ); + //Log.e(TAG, "TYPE_WINDOW_CONTENT_CHANGED " ); if(event.getClassName().toString().equals("androidx.viewpager.widget.ViewPager") && flag){ clickEdit(); } break; case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED://界面变化事件 - Log.e(TAG, "TYPE_WINDOW_STATE_CHANGED " ); + //Log.e(TAG, "TYPE_WINDOW_STATE_CHANGED " ); if(source != null && event.getClassName().toString().equals("com.ss.android.ugc.aweme.live.LivePlayActivity") && flag){ clickEdit(); } @@ -100,17 +104,17 @@ } break; case AccessibilityEvent.TYPE_VIEW_FOCUSED://焦点变更 - Log.e(TAG, "TYPE_VIEW_FOCUSED ResourceName:" +( source == null ? null : source.getParent())); + //Log.e(TAG, "TYPE_VIEW_FOCUSED ResourceName:" +( source == null ? null : source.getParent())); if(event.getClassName().equals("android.widget.EditText") && source != null){ //source.performAction(AccessibilityNodeInfo.ACTION_LONG_CLICK); //source.performAction(AccessibilityNodeInfo.ACTION_PASTE);//粘贴 } break; case AccessibilityEvent.TYPE_VIEW_LONG_CLICKED: - Log.e(TAG, "TYPE_VIEW_LONG_CLICKED ResourceName:" +( source == null ? null : source.getParent())); + //Log.e(TAG, "TYPE_VIEW_LONG_CLICKED ResourceName:" +( source == null ? null : source.getParent())); break; case AccessibilityEvent.TYPE_VIEW_CLICKED: - Log.e(TAG, "TYPE_VIEW_CLICKED " ); + //Log.e(TAG, "TYPE_VIEW_CLICKED " ); if(nodeInfo != null) { Log.e(TAG, "TYPE_VIEW_CLICKED source:" + nodeInfo.getText() + " " + nodeInfo.getClass()); } @@ -119,7 +123,7 @@ break; case AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED: //界面文字改动 - Log.e(TAG, "TYPE_VIEW_TEXT_CHANGED " ); + //Log.e(TAG, "TYPE_VIEW_TEXT_CHANGED " ); break; } @@ -129,24 +133,26 @@ * 点击弹出 edit弹框 */ private void clickEdit(){ - List<AccessibilityNodeInfo> list = getRootInActiveWindow().findAccessibilityNodeInfosByText("说点什么..."); - Log.e(TAG,"获取到控件 "+list.size()); - if (list != null && list.size() > 0 && flag) { - //模拟第三方点击事件 - //找到你的节点以后 就直接点击他就行了 - //AccessibilityNodeInfo node = list.get(0); - list.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { - Log.e(TAG,"执行点击 "+list.get(0).getPaneTitle()); + try{ + List<AccessibilityNodeInfo> list = getRootInActiveWindow().findAccessibilityNodeInfosByText("说点什么..."); + Log.e(TAG,"获取到控件 "+list.size()); + if (list != null && list.size() > 0 && flag) { + //模拟第三方点击事件 + //找到你的节点以后 就直接点击他就行了 + //AccessibilityNodeInfo node = list.get(0); + list.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { + Log.e(TAG,"执行点击 "+list.get(0).getPaneTitle()); + } + }else if(flag){ + //Toast.makeText(getBaseContext(),"未找到控件",Toast.LENGTH_SHORT).show(); } - }else if(flag){ - //Toast.makeText(getBaseContext(),"未找到控件",Toast.LENGTH_SHORT).show(); - } + }catch ( Exception e){} } @SuppressLint("NewApi") private void printView(AccessibilityNodeInfo node, String space){ - Log.e(TAG,space +node+" id:"+node.getViewIdResourceName() +" class:"+node.getClassName()+" text:"+node.getText()+" "+node.getPaneTitle()); + //Log.e(TAG,space +node+" id:"+node.getViewIdResourceName() +" class:"+node.getClassName()+" text:"+node.getText()+" "+node.getPaneTitle()); if(node.getChildCount() > 0){ for(int i = 0 ; i < node.getChildCount() ; i ++){ if(node.getChild(i) != null) { @@ -157,7 +163,7 @@ } @SuppressLint("NewApi") private AccessibilityNodeInfo getView(AccessibilityNodeInfo node, String className){ - Log.e(TAG,className +" id:"+node.getViewIdResourceName() +" class:"+node.getClassName()+" text:"+node.getText()+" "+node.getPaneTitle()); + //Log.e(TAG,className +" id:"+node.getViewIdResourceName() +" class:"+node.getClassName()+" text:"+node.getText()+" "+node.getPaneTitle()); if(node.getChildCount() > 0){ for(int i = 0 ; i < node.getChildCount() ; i ++){ if(node.getChild(i) != null) { @@ -176,8 +182,8 @@ @SuppressLint("NewApi") private AccessibilityNodeInfo getViewByDesc(AccessibilityNodeInfo node, String desc){ try{ - Log.e(TAG,desc +" getViewByDesc id:"+node.getViewIdResourceName() +" class:"+node.getClassName()+" text:"+node.getText()+" "+node.getContentDescription()); - Log.e(TAG,"getViewByDesc node:"+node); + //Log.e(TAG,desc +" getViewByDesc id:"+node.getViewIdResourceName() +" class:"+node.getClassName()+" text:"+node.getText()+" "+node.getContentDescription()); + //Log.e(TAG,"getViewByDesc node:"+node); if(node.getChildCount() > 0){ for(int i = 0 ; i < node.getChildCount() ; i ++){ if(node.getChild(i) != null) { @@ -209,7 +215,9 @@ @Override public void onInterrupt() { + Log.e(TAG,"onInterrupt 服务停止"); unregisterReceiver(receiver); + Toast.makeText(getBaseContext(),"服务停止",Toast.LENGTH_SHORT).show(); } @Override protected void onServiceConnected() { -- Gitblit v1.9.1