package com.hdl.photovoltaic.internet.HttpServer;
|
|
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);
|
|
private final IBinder mBinder = new LocalBinder();
|
|
@Override
|
public void onCreate() {
|
super.onCreate();
|
try {
|
myNanoHttpServer.start();
|
} catch (Exception e) {
|
// 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("初始化Http服务器失败---" + e.getMessage(), true);
|
startService(new Intent(this, MyNanoHttpService.class));
|
}
|
}
|
|
|
@Override
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
return super.onStartCommand(intent, flags, startId);
|
}
|
|
|
@Override
|
public void onDestroy() {
|
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;
|
}
|
}
|
}
|