| | |
| | | package cn.sinata.rxnetty; |
| | | |
| | | import static cn.sinata.rxnetty.LogHelper.logRecord; |
| | | |
| | | import android.app.*; |
| | | import android.content.Context; |
| | | import android.content.Intent; |
| | |
| | | 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 java.nio.charset.Charset; |
| | | import java.util.ArrayList; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | import cn.sinata.rxnetty.netStatus.NetChangeObserver; |
| | | import cn.sinata.rxnetty.netStatus.NetStateReceiver; |
| | | import cn.sinata.rxnetty.netStatus.NetUtils; |
| | |
| | | */ |
| | | public class CoreService extends Service { |
| | | |
| | | private String TAG = "CoreService"; |
| | | |
| | | @Nullable |
| | | @Override |
| | | public IBinder onBind(Intent intent) { |
| | |
| | | @Override |
| | | public void onCreate() { |
| | | super.onCreate(); |
| | | logRecord(TAG, "onCreate"); |
| | | } |
| | | |
| | | void init() { |
| | |
| | | @Override |
| | | public void onSend(String s) { |
| | | //先检查通道状态,如果断开,则重连。 |
| | | checkState(); |
| | | boolean state = checkState(); |
| | | if (!state) { |
| | | reConnect(); |
| | | } |
| | | Observable<Void> send = send(s + "\n"); |
| | | // Observable<Void> send = send(s); |
| | | if (send != null) { |
| | |
| | | initCheckOb(); |
| | | } |
| | | |
| | | public void checkState() { |
| | | public boolean checkState() { |
| | | if (mConnection == null || mConnection.getChannel() == null |
| | | || !mConnection.getChannel().isActive() |
| | | || !mConnection.getChannel().isWritable()) { |
| | | reConnect(); |
| | | logRecord(TAG, "checkState need reConnect"); |
| | | return false; |
| | | } else { |
| | | logRecord(TAG, "checkState no need reConnect"); |
| | | return true; |
| | | } |
| | | } |
| | | |
| | | void initCheckOb() { |
| | | NettyClient.getInstance().setOnCheckListener(new OnCheckListener() { |
| | | @Override |
| | | public void doCheck() { |
| | | checkState(); |
| | | public boolean doCheck() { |
| | | boolean state = checkState(); |
| | | if (!state) { |
| | | reConnect(); |
| | | } |
| | | return state; |
| | | } |
| | | }); |
| | | } |
| | |
| | | @Override |
| | | public void onNetConnected(NetUtils.NetType type) { |
| | | super.onNetConnected(type); |
| | | checkState(); |
| | | logRecord(TAG, "onNetConnected start checkState"); |
| | | boolean state = checkState(); |
| | | if (!state) { |
| | | reConnect(); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void onNetDisConnect() { |
| | | super.onNetDisConnect(); |
| | | logRecord(TAG, "onNetDisConnect"); |
| | | } |
| | | }; |
| | | NetStateReceiver.registerObserver(mNetChangeObserver); |
| | | if (Config.isStartForeground) { |
| | | if (Build.VERSION.SDK_INT >= 34) { |
| | | startForeground(Config.NOTIFICATION_ID,getNotification(), ServiceInfo.FOREGROUND_SERVICE_TYPE_REMOTE_MESSAGING); |
| | | startForeground(Config.NOTIFICATION_ID, getNotification(), ServiceInfo.FOREGROUND_SERVICE_TYPE_REMOTE_MESSAGING); |
| | | } else { |
| | | startForeground(Config.NOTIFICATION_ID,getNotification()); |
| | | startForeground(Config.NOTIFICATION_ID, getNotification()); |
| | | } |
| | | } |
| | | return START_NOT_STICKY; |
| | | } |
| | | |
| | | private Notification getNotification(){ |
| | | private Notification getNotification() { |
| | | if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { |
| | | //8.0以上创建channel |
| | | NotificationManager notificationManager = |
| | |
| | | intent.setClassName(getPackageName(), getPackageName() + ".broadcast.MBroadcastReceiver"); |
| | | PendingIntent contentIntent; |
| | | if (Build.VERSION.SDK_INT >= 31) { |
| | | contentIntent= PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_IMMUTABLE); |
| | | }else { |
| | | contentIntent= PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); |
| | | contentIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_IMMUTABLE); |
| | | } else { |
| | | contentIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); |
| | | } |
| | | mBuilder.setContentIntent(contentIntent); |
| | | return mBuilder.build(); |
| | |
| | | public void onDestroy() { |
| | | super.onDestroy(); |
| | | isDestroy = true; |
| | | logRecord(TAG, "onDestroy"); |
| | | NetStateReceiver.unRegisterNetworkStateReceiver(this); |
| | | if (subscriber != null && !subscriber.isUnsubscribed()) { |
| | | subscriber.unsubscribe(); |
| | |
| | | } |
| | | |
| | | private void connectServer() { |
| | | |
| | | if (NetUtils.isNetworkAvailable(this)) { |
| | | boolean status = NetUtils.isNetworkAvailable(this); |
| | | logRecord(TAG, "connectServer net status = " + status); |
| | | if (status) { |
| | | connectionSub(); |
| | | connect(Config.SOCKET_SERVER, Config.SOCKET_PORT) |
| | | .subscribeOn(Schedulers.io()) |
| | |
| | | @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; |
| | | } |
| | |
| | | mConnection.close(); |
| | | mConnection = null; |
| | | } |
| | | Log.i("CoreService", "sendHeart_websocket长连接断开,重新连接"); |
| | | logRecord(TAG, "reConnect sendHeart_websocket长连接断开,重新连接"); |
| | | connectServer(); |
| | | } |
| | | }); |