18034727885
2026-03-18 7804ce8edeba51418b5fde2d306e5f476dfb5993
rxnetty/src/main/java/cn/sinata/rxnetty/NettyClient.java
@@ -1,10 +1,11 @@
package cn.sinata.rxnetty;
import static cn.sinata.rxnetty.LogHelper.logRecord;
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;
@@ -13,23 +14,23 @@
 */
public class NettyClient {
    private String TAG = "NettyClient";
    public boolean isStop = false;
    private Context mContext;
    private ArrayList<OnMessageListener> listeners ;
    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) {
        init(context, server, port, false);
    }
    public void init(Context context,String server,int port,boolean isStartForeground) {
    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;
        Config.isStartForeground = isStartForeground;
    }
    public OnConnectListener getConnectListener() {
@@ -49,9 +50,9 @@
    }
    /**
     * 添加消息监听
     *
     * @param listener
     */
    public void addOnMessageListener(OnMessageListener listener) {
@@ -65,6 +66,7 @@
    /**
     * 连接完成监听
     *
     * @param listener
     */
    public void setOnConnectListener(OnConnectListener listener) {
@@ -72,7 +74,8 @@
    }
    /**
     *  移除消息监听
     * 移除消息监听
     *
     * @param listener
     */
    public void removeOnMessageListener(OnMessageListener listener) {
@@ -94,10 +97,11 @@
    /**
     * 发送消息给服务器
     *
     * @param msg
     */
    public void sendMessage(String msg) {
        Log.e("sendLocation: ","发送消息" +msg);
        logRecord(TAG, "sendMessage sendListener is null " + (sendListener == null) + " msg = " + msg);
        if (sendListener != null) {
            sendListener.onSend(msg);
        }
@@ -106,9 +110,12 @@
    /**
     * 检测netty连接状况
     */
    public void checkNettyState() {
    public boolean checkNettyState() {
        logRecord(TAG, "checkNettyState onCheckListener is null " + (onCheckListener == null));
        if (onCheckListener != null) {
            onCheckListener.doCheck();
            return onCheckListener.doCheck();
        } else {
            return false;
        }
    }
@@ -117,7 +124,7 @@
            return;
        }
        if (!isStop) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)  {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                try {
                    Intent intent = new Intent(this.getContext(), NJobService.class);
                    this.mContext.stopService(intent);
@@ -129,7 +136,7 @@
            }
        }
        isStop = false;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)  {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            try {
                Intent intent = new Intent(this.getContext(), NJobService.class);
                this.mContext.startService(intent);
@@ -146,11 +153,10 @@
            return;
        }
        isStop = true;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)  {
        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);
                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) {