From 5d3efa4c93dde0cde474951e5310bb72ebbf4184 Mon Sep 17 00:00:00 2001 From: wjc <1243177876@qq.com> Date: 星期三, 07 五月 2025 15:18:26 +0800 Subject: [PATCH] 2025年05月07日15:18:20 1.2.0 --- app/src/main/java/com/hdl/photovoltaic/internet/HttpServer/MyNanoHttpService.java | 52 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 files changed, 46 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/hdl/photovoltaic/internet/HttpServer/MyNanoHttpService.java b/app/src/main/java/com/hdl/photovoltaic/internet/HttpServer/MyNanoHttpService.java index 007d757..99228dc 100644 --- a/app/src/main/java/com/hdl/photovoltaic/internet/HttpServer/MyNanoHttpService.java +++ b/app/src/main/java/com/hdl/photovoltaic/internet/HttpServer/MyNanoHttpService.java @@ -2,18 +2,26 @@ import android.app.Service; import android.content.Intent; +import android.os.Binder; import android.os.IBinder; +import android.text.TextUtils; import androidx.annotation.Nullable; +import com.hdl.photovoltaic.other.HdlLogLogic; + +import org.w3c.dom.Text; + +import java.io.IOException; +import java.util.Objects; + +/** + * 鏈湴鏈嶅姟鍣� + */ public class MyNanoHttpService extends Service { private MyNanoHttpServer myNanoHttpServer = MyNanoHttpServer.getInstance(null); - @Nullable - @Override - public IBinder onBind(Intent intent) { - return null; - } + private final IBinder mBinder = new LocalBinder(); @Override public void onCreate() { @@ -21,7 +29,26 @@ try { myNanoHttpServer.start(); } catch (Exception e) { - e.printStackTrace(); +// if (myNanoHttpServer!=null&&myNanoHttpServer.gserverSocket.isBound() && !serverSocket.isClosed()) { +// // Port is available +// } else { +// // Port is not available +// } +// e.printStackTrace(); +// if (!TextUtils.isEmpty(e.getMessage()) && Objects.requireNonNull(e.getMessage()).contains("Address already in use")) { +// myNanoHttpServer = null; +// for (int i = MyNanoHttpServer.HTTP_PORT + 1; i < 65535; i++) { +// try { +// MyNanoHttpServer.HTTP_PORT = i; +// myNanoHttpServer = MyNanoHttpServer.getInstance(null); +// myNanoHttpServer.start(); +// } catch (Exception ex) { +// String s = ex.getMessage(); +// } +// } +// +// } + HdlLogLogic.print("鍒濆鍖朒ttp鏈嶅姟鍣ㄥけ璐�---" + e.getMessage(), true); startService(new Intent(this, MyNanoHttpService.class)); } } @@ -38,4 +65,17 @@ super.onDestroy(); myNanoHttpServer.stop(); } + + @Nullable + @Override + public IBinder onBind(Intent intent) { + return mBinder; + } + + public class LocalBinder extends Binder { + public MyNanoHttpService getService() { + // Return this instance of LocalService so clients can call public methods + return MyNanoHttpService.this; + } + } } -- Gitblit v1.8.0