mac
2023-12-21 78dbac2e8e7ad58d9e5d6a025de0d6fc58f69146
app/src/main/java/com/hdl/photovoltaic/internet/HttpServer/MyNanoHttpService.java
@@ -2,18 +2,18 @@
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import androidx.annotation.Nullable;
/**
 * 本地服务器
 */
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,6 +21,11 @@
        try {
            myNanoHttpServer.start();
        } catch (Exception e) {
//            if (serverSocket.isBound() && !serverSocket.isClosed()) {
//                // Port is available
//            } else {
//                // Port is not available
//            }
            e.printStackTrace();
            startService(new Intent(this, MyNanoHttpService.class));
        }
@@ -38,4 +43,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;
        }
    }
}