| | |
| | | package cn.sinata.rxnetty; |
| | | |
| | | 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.graphics.Color; |
| | | import android.os.Build; |
| | | import android.os.IBinder; |
| | | import android.util.Log; |
| | | |
| | |
| | | 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; |
| | |
| | | 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; |
| | |
| | | }; |
| | | NetStateReceiver.registerObserver(mNetChangeObserver); |
| | | if (Config.isStartForeground) { |
| | | 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 = |
| | |
| | | return; |
| | | } |
| | | //reconnect |
| | | Observable.timer(3, TimeUnit.SECONDS).subscribe(new Action1<Long>() { |
| | | @Override |
| | | public void call(Long aLong) { |
| | | if (mConnection != null) { |
| | | mConnection.close(); |
| | | mConnection = null; |
| | | } |
| | | Log.i("CoreService", "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); |
| | | |
| | | } |
| | | |