18034727885
2026-08-14 ab732421b3d37097458adeca9a12f2df6a15b30a
rxnetty/src/main/java/cn/sinata/rxnetty/CoreService.java
@@ -1,14 +1,17 @@
package cn.sinata.rxnetty;
import static cn.sinata.rxnetty.LogHelper.logRecord;
import android.app.*;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ServiceInfo;
import android.graphics.Color;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
@@ -26,6 +29,7 @@
import io.reactivex.netty.channel.ObservableConnection;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;
@@ -46,7 +50,6 @@
    @Override
    public void onCreate() {
        super.onCreate();
        logRecord(TAG, "onCreate");
    }
    void init() {
@@ -88,10 +91,8 @@
        if (mConnection == null || mConnection.getChannel() == null
                || !mConnection.getChannel().isActive()
                || !mConnection.getChannel().isWritable()) {
            logRecord(TAG, "checkState need reConnect");
            return false;
        } else {
            logRecord(TAG, "checkState no need reConnect");
            return true;
        }
    }
@@ -117,11 +118,15 @@
        init();
        NetStateReceiver.registerNetworkStateReceiver(this);
        // 服务重复启动时先移除旧观察者,避免重复重连回调 由AI生成
        if (mNetChangeObserver != null) {
            // 从网络监听器移除旧观察者实例 由AI生成
            NetStateReceiver.removeRegisterObserver(mNetChangeObserver);
        }
        mNetChangeObserver = new NetChangeObserver() {
            @Override
            public void onNetConnected(NetUtils.NetType type) {
                super.onNetConnected(type);
                logRecord(TAG, "onNetConnected start checkState");
                boolean state = checkState();
                if (!state) {
                    reConnect();
@@ -131,7 +136,6 @@
            @Override
            public void onNetDisConnect() {
                super.onNetDisConnect();
                logRecord(TAG, "onNetDisConnect");
            }
        };
        NetStateReceiver.registerObserver(mNetChangeObserver);
@@ -187,7 +191,8 @@
    public void onDestroy() {
        super.onDestroy();
        isDestroy = true;
        logRecord(TAG, "onDestroy");
        // 服务销毁时移除网络观察者,避免静态列表持有 Service 引用 由AI生成
        NetStateReceiver.removeRegisterObserver(mNetChangeObserver);
        NetStateReceiver.unRegisterNetworkStateReceiver(this);
        if (subscriber != null && !subscriber.isUnsubscribed()) {
            subscriber.unsubscribe();
@@ -202,7 +207,6 @@
    private void connectServer() {
        boolean status = NetUtils.isNetworkAvailable(this);
        logRecord(TAG, "connectServer net status = " + status);
        if (status) {
            connectionSub();
            connect(Config.SOCKET_SERVER, Config.SOCKET_PORT)
@@ -230,7 +234,6 @@
            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                logRecord(TAG, "connectionSub onError " + e.getMessage());
                reConnect();
            }
@@ -261,7 +264,6 @@
            @Override
            public void onError(Throwable e) {
                logRecord(TAG, "initReceiveOb onError " + e.getMessage());
                reConnect();
            }
@@ -287,22 +289,24 @@
    private void reConnect() {
        logRecord(TAG, "reConnect isDestroy = " + isDestroy);
        if (isDestroy) {
            return;
        }
        //reconnect
        Observable.timer(3, TimeUnit.SECONDS).subscribe(new Action1<Long>() {
            @Override
            public void call(Long aLong) {
                if (mConnection != null) {
                    mConnection.close();
                    mConnection = null;
                }
                logRecord(TAG, "reConnect sendHeart_websocket长连接断开,重新连接");
                connectServer();
            }
        });
        Observable.timer(3, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<Long>() {
                    @Override
                    public void call(Long aLong) {
                        if (mConnection != null) {
                            mConnection.close();
                            mConnection = null;
                        }
                        Log.i("CoreService", "sendHeart_websocket长连接断开,重新连接");
                        connectServer();
                    }
                }, Throwable::printStackTrace);
    }