| | |
| | | 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; |
| | |
| | | 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; |
| | |
| | | @Override |
| | | public void onCreate() { |
| | | super.onCreate(); |
| | | logRecord(TAG, "onCreate"); |
| | | } |
| | | |
| | | void init() { |
| | |
| | | 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; |
| | | } |
| | | } |
| | |
| | | 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(); |
| | |
| | | @Override |
| | | public void onNetDisConnect() { |
| | | super.onNetDisConnect(); |
| | | logRecord(TAG, "onNetDisConnect"); |
| | | } |
| | | }; |
| | | NetStateReceiver.registerObserver(mNetChangeObserver); |
| | |
| | | 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(); |
| | |
| | | |
| | | private void connectServer() { |
| | | boolean status = NetUtils.isNetworkAvailable(this); |
| | | logRecord(TAG, "connectServer net status = " + status); |
| | | if (status) { |
| | | connectionSub(); |
| | | connect(Config.SOCKET_SERVER, Config.SOCKET_PORT) |
| | |
| | | @Override |
| | | public void onError(Throwable e) { |
| | | e.printStackTrace(); |
| | | logRecord(TAG, "connectionSub onError " + e.getMessage()); |
| | | reConnect(); |
| | | } |
| | | |
| | |
| | | |
| | | @Override |
| | | public void onError(Throwable e) { |
| | | logRecord(TAG, "initReceiveOb onError " + e.getMessage()); |
| | | reConnect(); |
| | | } |
| | | |
| | |
| | | |
| | | |
| | | 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); |
| | | |
| | | } |
| | | |