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/MainActivity.java | 2 app/src/main/java/com/auto/lyric/service/FloatingWindowService.java | 103 ++++++++++++++++----------------- app/src/main/java/com/auto/lyric/service/AutoInputService.java | 34 ++++++----- 3 files changed, 70 insertions(+), 69 deletions(-) diff --git a/app/src/main/java/com/auto/lyric/MainActivity.java b/app/src/main/java/com/auto/lyric/MainActivity.java index ece5151..90e69f3 100644 --- a/app/src/main/java/com/auto/lyric/MainActivity.java +++ b/app/src/main/java/com/auto/lyric/MainActivity.java @@ -136,7 +136,7 @@ startActivity(intent); }else{//开启监听服务 startService(floatService); - startService(new Intent(this,AutoInputService.class)); + //startService(new Intent(this,AutoInputService.class)); } //startService(floatService); } 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 9e90755..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; } @@ -148,7 +152,7 @@ @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) { @@ -159,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) { diff --git a/app/src/main/java/com/auto/lyric/service/FloatingWindowService.java b/app/src/main/java/com/auto/lyric/service/FloatingWindowService.java index f6056fd..865ebe0 100644 --- a/app/src/main/java/com/auto/lyric/service/FloatingWindowService.java +++ b/app/src/main/java/com/auto/lyric/service/FloatingWindowService.java @@ -7,9 +7,9 @@ import android.content.Intent; import android.graphics.PixelFormat; import android.os.Build; -import android.os.CountDownTimer; import android.os.Handler; import android.os.IBinder; +import android.os.Looper; import android.os.Message; import android.util.Log; import android.view.Gravity; @@ -27,6 +27,7 @@ import com.auto.lyric.databinding.FloatViewBinding; import com.auto.lyric.util.DeviceUtil; +import java.lang.ref.WeakReference; import java.text.SimpleDateFormat; import java.util.Timer; import java.util.TimerTask; @@ -41,23 +42,21 @@ private WindowManager.LayoutParams params; FloatViewBinding binding; final String TAG = "FloatingWindowService"; - final int THREAD_STOP = 0, KEYBOARD_SEND = 100; + final int THREAD_STOP = 99, KEYBOARD_SEND = 100; SimpleDateFormat msFormat = new SimpleDateFormat("mm:ss.SSS"); boolean pause;//是否暂停 int progress;//时间进度 LyricObject lyricObject;//当前发送过的歌词 - Handler handler; + MyHandler handler ; //计时器 - Timer timer; - CountDownTimer countDownTimer; - ServiceTask task;//计时器执行任务 + Timer timer = new Timer(); + //计时器执行任务 class ServiceTask extends TimerTask{ @Override public void run() { int index = binding.lyric.getIndex(progress+=10); - binding.lyric.setOffsetY(220 - index * (binding.lyric.getSIZEWORD() + 44)); - binding.lyric.invalidate(); + handler.sendMessage(new Message()); if(lyricObject == null || lyricObject != LyricServer.getLrc_map().get(index)){ lyricObject = LyricServer.getLrc_map().get(index); Message message = new Message(); @@ -138,6 +137,7 @@ stop(); } }); + handler = new MyHandler(Looper.getMainLooper(),this); } @Nullable @@ -146,28 +146,6 @@ return null; } - private void initHandler(){ - handler = new Handler(){ - @Override - public void handleMessage(@NonNull Message msg) { - super.handleMessage(msg); - if(msg.what == THREAD_STOP){//停止 - stop(); - }else if(msg.what == KEYBOARD_SEND){//发送文本 - copy(msg.obj.toString()); - Intent intent = new Intent(); - intent.setAction(AutoInputService.class.getName()); - intent.putExtra("action",AutoInputService.ACTION_PASTE); - sendBroadcast(intent); - intent.putExtra("action",AutoInputService.ACTION_SEND); - sendBroadcast(intent); - }else if(msg.what == -1){//显示在输入框中 - - //Toast.makeText(getBaseContext(),"上一句--"+msg.obj,Toast.LENGTH_SHORT).show(); - } - } - }; - } @Override public int onStartCommand(Intent intent, int flags, int startId) { @@ -176,7 +154,6 @@ manager.addView(binding.getRoot(), params); } binding.lyric.setTextSize(); - initHandler(); stop(); progress = LyricServer.getLrc_map().get(0).begintime; binding.timer.setText(msFormat.format(progress)); @@ -194,18 +171,18 @@ case MotionEvent.ACTION_MOVE: touchY=tt-touchY; binding.lyric.setOffsetY(binding.lyric.getOffsetY()+touchY); - Log.e("LyricView","offsety:"+binding.lyric.getOffsetY()); + //Log.e("LyricView","offsety:"+binding.lyric.getOffsetY()); binding.lyric.invalidate(); break; case MotionEvent.ACTION_UP: if(binding.btnStart.getText().equals("开启") || pause) { int index = binding.lyric.getIndexFromOffsetY(binding.lyric.getOffsetY()); LyricObject lyricObject = LyricServer.getLrc_map().get(index); - Log.e("LyricView", "index:"+index+" object:" + lyricObject); + //Log.e("LyricView", "index:"+index+" object:" + lyricObject); if(lyricObject != null) { binding.lyric.setIndex(index); binding.lyric.setOffsetY(220 - index * (binding.lyric.getSIZEWORD() + 44)); - Log.e("LyricView", "object:" + lyricObject); + //Log.e("LyricView", "object:" + lyricObject); progress = lyricObject.begintime; binding.timer.setText(msFormat.format(progress)); } @@ -226,9 +203,9 @@ Log.e(TAG,"onDestroy "); try { timer.cancel(); - countDownTimer.cancel(); }catch (Exception e){} AutoInputService.flag = false; + pause = true; manager.removeView(binding.getRoot()); } @@ -237,37 +214,25 @@ */ private void start(){ AutoInputService.flag = true; + pause = false; try { timer.cancel(); - countDownTimer.cancel(); }catch (Exception e){} - countDownTimer = new CountDownTimer(LyricServer.getLrc_map().get(LyricServer.getLrc_map().keySet().size()-1).begintime,10) { - @Override - public void onTick(long millisUntilFinished) { - binding.timer.setText(msFormat.format(progress)); - } - - @Override - public void onFinish() { - - } - }; - countDownTimer.start(); - timer = new Timer(); - task = new ServiceTask(); - timer.schedule(task,0,10); + timer.schedule(new ServiceTask(),0,10); binding.btnStart.setText("停止"); binding.btnPause.setText("暂停"); binding.btnBack.setEnabled(true); binding.btnFast.setEnabled(true); binding.btnPause.setEnabled(true); - pause = false; + Intent intent = new Intent(); + intent.setAction(AutoInputService.class.getName()); + intent.putExtra("action",AutoInputService.ACTION_START); + sendBroadcast(intent); } private void pause(){ try { timer.cancel(); - countDownTimer.cancel(); }catch (Exception e){} pause = true; AutoInputService.flag = false; @@ -294,5 +259,37 @@ clipboard.setPrimaryClip(clipData); } + class MyHandler extends Handler{ + + private final WeakReference<FloatingWindowService> weakReference; + + MyHandler(Looper looper,FloatingWindowService service) { + super(looper); + this.weakReference = new WeakReference<>(service); + } + + @Override + public void handleMessage(@NonNull Message msg) { + super.handleMessage(msg); + if(msg.what == THREAD_STOP){//停止 + stop(); + }else if(msg.what == KEYBOARD_SEND){//发送文本 + copy(msg.obj.toString()); + Intent intent = new Intent(); + intent.setAction(AutoInputService.class.getName()); + intent.putExtra("action",AutoInputService.ACTION_PASTE); + sendBroadcast(intent); + intent.putExtra("action",AutoInputService.ACTION_SEND); + sendBroadcast(intent); + }else if(msg.what == -1){//显示在输入框中 + + //Toast.makeText(getBaseContext(),"上一句--"+msg.obj,Toast.LENGTH_SHORT).show(); + } + int index = binding.lyric.getIndex(progress); + binding.lyric.setOffsetY(220 - index * (binding.lyric.getSIZEWORD() + 44)); + binding.lyric.invalidate(); + weakReference.get().binding.timer.setText(msFormat.format(progress)); + } + } } -- Gitblit v1.9.1