mac
2023-11-30 caa35eaa7d9cae55a57155d9508d1285999f1bba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.hdl.photovoltaic.internet.HttpServer;
 
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);
 
    private final IBinder mBinder = new LocalBinder();
 
    @Override
    public void onCreate() {
        super.onCreate();
        try {
            myNanoHttpServer.start();
        } catch (Exception e) {
            e.printStackTrace();
            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;
        }
    }
}