| | |
| | | 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; |
| | |
| | | @Override |
| | | public void onCreate() { |
| | | super.onCreate(); |
| | | logRecord("onCreate"); |
| | | } |
| | | |
| | | void init() { |
| | |
| | | if (mConnection == null || mConnection.getChannel() == null |
| | | || !mConnection.getChannel().isActive() |
| | | || !mConnection.getChannel().isWritable()) { |
| | | logRecord("checkState need reConnect"); |
| | | return false; |
| | | } else { |
| | | logRecord("checkState no need reConnect"); |
| | | return true; |
| | | } |
| | | } |
| | |
| | | @Override |
| | | public void onNetConnected(NetUtils.NetType type) { |
| | | super.onNetConnected(type); |
| | | logRecord("onNetConnected start checkState"); |
| | | boolean state = checkState(); |
| | | if (!state) { |
| | | reConnect(); |
| | |
| | | @Override |
| | | public void onNetDisConnect() { |
| | | super.onNetDisConnect(); |
| | | logRecord("onNetDisConnect"); |
| | | } |
| | | }; |
| | | NetStateReceiver.registerObserver(mNetChangeObserver); |
| | |
| | | public void onDestroy() { |
| | | super.onDestroy(); |
| | | isDestroy = true; |
| | | logRecord("onDestroy"); |
| | | NetStateReceiver.unRegisterNetworkStateReceiver(this); |
| | | if (subscriber != null && !subscriber.isUnsubscribed()) { |
| | | subscriber.unsubscribe(); |
| | |
| | | |
| | | private void connectServer() { |
| | | boolean status = NetUtils.isNetworkAvailable(this); |
| | | logRecord("connectServer net status = " + status); |
| | | if (status) { |
| | | connectionSub(); |
| | | connect(Config.SOCKET_SERVER, Config.SOCKET_PORT) |
| | |
| | | @Override |
| | | public void onError(Throwable e) { |
| | | e.printStackTrace(); |
| | | logRecord("connectionSub onError " + e.getMessage()); |
| | | reConnect(); |
| | | } |
| | | |
| | |
| | | |
| | | @Override |
| | | public void onError(Throwable e) { |
| | | logRecord("initReceiveOb onError " + e.getMessage()); |
| | | reConnect(); |
| | | } |
| | | |
| | |
| | | |
| | | |
| | | private void reConnect() { |
| | | logRecord("reConnect isDestroy = " + isDestroy); |
| | | if (isDestroy) { |
| | | return; |
| | | } |
| | |
| | | mConnection.close(); |
| | | mConnection = null; |
| | | } |
| | | logRecord("reConnect sendHeart_websocket长连接断开,重新连接"); |
| | | connectServer(); |
| | | } |
| | | }); |