瀏覽代碼

控制连接次数。连接超时时不再连接

cs 9 月之前
父節點
當前提交
2926c46a66

+ 7 - 1
app/src/main/java/org/las2mile/okio/OkIoClient.java

@@ -28,12 +28,14 @@ import org.las2mile.ui.LogDlMsgEvent;
 import org.las2mile.ui.LogEndStickyMsgEvent;
 import org.las2mile.ui.ImgEndStickyMsgEvent;
 import org.greenrobot.eventbus.EventBus;
+import java.util.concurrent.atomic.AtomicBoolean;
 
 public class OkIoClient extends Thread{
     private String serverIP;
     private ExecutorService executor;
     private BufferedSink mBufferedSink;
     Map<Integer,DownTaskHandler> peddingDownloadTasks;
+    private AtomicBoolean LetServceRunning = new AtomicBoolean(true);
 
     public OkIoClient(String sip){
         serverIP = sip;
@@ -41,12 +43,16 @@ public class OkIoClient extends Thread{
         this.executor = Executors.newCachedThreadPool();
     }
 
+    public void stopOkIoClient() {
+        LetServceRunning.set(false);
+    }
+
     @Override
     public void run() {
         super.run();
         int attempts = 50;
         Socket socket =  null;
-        while (attempts != 0) {
+        while (attempts != 0 && LetServceRunning.get()) {
         try {
             Ln.e("exter tcp client try connect "+attempts);
             socket = new Socket(serverIP, 4567);

+ 12 - 7
app/src/main/java/org/las2mile/scrcpy/MainActivity.java

@@ -206,8 +206,10 @@ public class MainActivity extends FragmentActivity implements Scrcpy.ServiceCall
                        serviceBound = false;
                        scrcpy_main();
                    }
-                   Toast.makeText(context, "Connection Timed out", Toast.LENGTH_SHORT).show();
+                   Toast.makeText(context, serviceBound+" Connection Timed out", Toast.LENGTH_SHORT).show();
                    //连接超时
+                   mainHandler.sendEmptyMessage(ADB_CONNECT_FAIL);
+                   scrcpy.StopService();
                }else{
                int[] rem_res = scrcpy.get_remote_device_resolution();
                remote_device_height = rem_res[1];
@@ -438,12 +440,15 @@ public class MainActivity extends FragmentActivity implements Scrcpy.ServiceCall
             final Button backButton = findViewById(R.id.back_button);
             final Button homeButton = findViewById(R.id.home_button);
             final Button appswitchButton = findViewById(R.id.appswitch_button);
-
-            backButton.setOnClickListener(v -> scrcpy.sendKeyevent(4));
-
-            homeButton.setOnClickListener(v -> scrcpy.sendKeyevent(3));
-
-            appswitchButton.setOnClickListener(v -> scrcpy.sendKeyevent(187));
+            if(backButton != null){
+                backButton.setOnClickListener(v -> scrcpy.sendKeyevent(4));
+            }
+            if(homeButton != null){
+                homeButton.setOnClickListener(v -> scrcpy.sendKeyevent(3));
+            }
+            if(appswitchButton != null){
+                appswitchButton.setOnClickListener(v -> scrcpy.sendKeyevent(187));
+            }
         }
         }
 

+ 7 - 4
app/src/main/java/org/las2mile/scrcpy/Scrcpy.java

@@ -114,6 +114,9 @@ public class Scrcpy extends Service {
     }
 
     public void StopService() {
+        if(mOkIoClient != null){
+            mOkIoClient.stopOkIoClient();
+        }
         LetServceRunning.set(false);
         stopSelf();
     }
@@ -164,9 +167,9 @@ public class Scrcpy extends Service {
         Socket socket = null;
         VideoPacket.StreamSettings streamSettings = null;
         int attempts = 50;
-        while (attempts != 0) {
+        while (attempts != 0 && LetServceRunning.get()) {
             try {
-                Log.e("Scrcpy",attempts+" Connecting to screen push server port 7007:" + serverAdr);
+                //Log.e("Scrcpy",attempts+" Connecting to screen push server");
                 socket = new Socket(serverAdr, 7007);
                 dataInputStream = new DataInputStream(socket.getInputStream());
                 dataOutputStream = new DataOutputStream(socket.getOutputStream());
@@ -251,7 +254,7 @@ public class Scrcpy extends Service {
                     Thread.sleep(300);//连接比较耗时,适当延长重连时间。连接服务器7007端口,此端口用于推屏
                 } catch (InterruptedException ignore) {
                 }
-//                 Log.e("Scrcpy", e.getMessage());
+                Log.e("Scrcpy", "can't connect the device,tryed "+attempts);
             } finally {
                 if (socket != null) {
                     try {
@@ -264,7 +267,7 @@ public class Scrcpy extends Service {
             }
 
         }
-
+        //Log.e("Scrcpy", "what====================== ");
     }
 
     public interface ServiceCallbacks {