lmw
2023-05-27 ff365ff4346d220edf2ec1d0041f2284befe3870
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package cn.sinata.rxnetty;
 
import android.app.job.JobScheduler;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.util.Log;
 
import java.util.ArrayList;
 
/**
 * 使用的是rxjava1。而不是rxjava2。所以尽量不要在主项目中使用rxjava1相关类。以免交叉。不好维护修改。
 */
 
public class NettyClient {
 
    public boolean isStop = false;
    private Context mContext;
    private ArrayList<OnMessageListener> listeners ;
    private OnSendListener sendListener;
    private OnConnectListener connectListener;
    private OnCheckListener onCheckListener;
 
        public void init(Context context,String server,int port) {
            init(context,server,port,false);
        }
 
    public void init(Context context,String server,int port,boolean isStartForeground) {
        mContext = context.getApplicationContext();
        Config.SOCKET_SERVER = server;
        Config.SOCKET_PORT = port;
        Config.isStartForeground= isStartForeground;
    }
 
    public OnConnectListener getConnectListener() {
        return connectListener;
    }
 
    private static final class Singleton {
        private final static NettyClient INSTANCE = new NettyClient();
    }
 
    public static NettyClient getInstance() {
        return Singleton.INSTANCE;
    }
 
    ArrayList<OnMessageListener> getListeners() {
        return listeners;
    }
 
 
 
    /**
     * 添加消息监听
     * @param listener
     */
    public void addOnMessageListener(OnMessageListener listener) {
        if (listeners == null) {
            listeners = new ArrayList<>();
        }
        if (listener != null && !listeners.contains(listener)) {
            listeners.add(listener);
        }
    }
 
    /**
     * 连接完成监听
     * @param listener
     */
    public void setOnConnectListener(OnConnectListener listener) {
        this.connectListener = listener;
    }
 
    /**
     *  移除消息监听
     * @param listener
     */
    public void removeOnMessageListener(OnMessageListener listener) {
        if (listeners == null) {
            return;
        }
        if (listener != null && listeners.contains(listener)) {
            listeners.remove(listener);
        }
    }
 
    void setSendListener(OnSendListener listener) {
        this.sendListener = listener;
    }
 
    void setOnCheckListener(OnCheckListener listener) {
        this.onCheckListener = listener;
    }
 
    /**
     * 发送消息给服务器
     * @param msg
     */
    public void sendMessage(String msg) {
        if (sendListener != null) {
            Log.e("Heart",msg);
            sendListener.onSend(msg);
        }
    }
 
    /**
     * 检测netty连接状况
     */
    public void checkNettyState() {
        if (onCheckListener != null) {
            onCheckListener.doCheck();
        }
    }
 
    public void startService() {
        if (mContext == null) {
            return;
        }
        if (!isStop) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)  {
                try {
                    Intent intent = new Intent(this.getContext(), NJobService.class);
                    this.mContext.stopService(intent);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                this.mContext.stopService(new Intent(this.mContext, CoreService.class));
            }
        }
        isStop = false;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)  {
            try {
                Intent intent = new Intent(this.getContext(), NJobService.class);
                this.mContext.startService(intent);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            this.mContext.startService(new Intent(this.mContext, CoreService.class));
        }
    }
 
    public void stopService() {
        if (mContext == null) {
            return;
        }
        isStop = true;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)  {
            try {
                JobScheduler jobScheduler = (JobScheduler) this.mContext.getSystemService(Context.JOB_SCHEDULER_SERVICE);
                if(jobScheduler!=null)
                    jobScheduler.cancel(11);
                this.mContext.stopService(new Intent(this.mContext, NJobService.class));
                this.mContext.stopService(new Intent(this.mContext, CoreService.class));
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            this.mContext.stopService(new Intent(this.mContext, CoreService.class));
        }
    }
 
    private Context getContext() {
        return mContext;
    }
}