+ * 【有道云笔记】学生项目-设备端websocket对接.md + * https://note.youdao.com/s/Ea0tebj8 + *
+ * 【有道云笔记】学生项目-管理端websocket对接.md + * https://note.youdao.com/s/MazEq49Q + *
+ * 老人在线websocket重构了,按照这个重新对接,跟之前售后差不多是一样的。 + *
+ * 1.设备未激活 不链接websocket 形成不了关联,设备激活后,才链接websocket
+ * 2.已激活的设备,设备若已链接websocket,用户确认绑定后,断开websocket 等待5秒 再重新链接websocket 以刷新用户关联
+ * 3.设备用户解除绑定后,断开websocket 等待5秒 再重新链接websocket 以刷新用户关联
+ */
+public class SocketService extends Service implements NetworkUtils.OnNetworkStatusChangedListener {
+ private final static String TAG = "JWebSocketClientService";
+
+ public JWebSocketClient mJWebSocketClient;
+// private SocketServiceBinder mBinder = new SocketServiceBinder();
+
+ @Override
+ public void onDisconnected() {
+ Log.i(TAG, "网络断开连接");
+ }
+
+ @Override
+ public void onConnected(NetworkUtils.NetworkType networkType) {
+ Log.i(TAG, "网络已连接");
+ }
+
+ //用于Activity和service通讯
+ public class SocketServiceBinder extends Binder {
+ public SocketService getService() {
+ return SocketService.this;
+ }
+ }
+
+// private ServiceConnection mServiceConnection = new ServiceConnection() {
+// @Override
+// public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
+// Log.e(TAG, "onServiceConnected: componentName = " + componentName);
+// boolean isServiceRunning = ServiceAliveUtils.isServiceAlive(SocketService.this, ManagerService.class.getName());
+// Log.e(TAG, "onServiceConnected: isServiceRunning = " + isServiceRunning);
+// if (!isServiceRunning) {
+// startService(new Intent(SocketService.this, ManagerService.class));
+// }
+// }
+//
+// @Override
+// public void onServiceDisconnected(ComponentName componentName) {
+// Log.e(TAG, "onServiceDisconnected: ");
+// // 断开链接
+// startService(new Intent(SocketService.this, ManagerService.class));
+// // 重新绑定
+// bindService(new Intent(SocketService.this, ManagerService.class), mServiceConnection, Context.BIND_IMPORTANT);
+// }
+// };
+
+ @Nullable
+ @Override
+ public IBinder onBind(Intent intent) {
+ return null;
+ }
+
+ @Override
+ public void onCreate() {
+ super.onCreate();
+ Log.e(TAG, "onCreate: ");
+ NetworkUtils.registerNetworkStatusChangedListener(this);
+ registerScreenLockReceiver();
+
+ if (ActivationUtil.isActivation(SocketService.this)) {
+ //初始化websocket
+ initSocketClient();
+ startLoop();
+ } else {
+ Log.e(TAG, "onCreate: 未激活不连接");
+ }
+// boolean isServiceRunning = ServiceAliveUtils.isServiceAlive(this, MainService.class.getName());
+// Log.e(TAG, "onCreate: isServiceRunning = " + isServiceRunning);
+// if (!isServiceRunning) {
+// startService(new Intent(this, MainService.class));
+// }
+// bindService(new Intent(this, ManagerService.class), mServiceConnection, Context.BIND_IMPORTANT);
+
+ mNotificationManagerCompat = NotificationManagerCompat.from(this);
+ createNotificationChannel();
+// showNotification();
+ sendSimpleNotification();
+ }
+
+ @Override
+ public int onStartCommand(Intent intent, int flags, int startId) {
+ Log.e(TAG, "onStartCommand: ");
+ return START_STICKY;
+ }
+
+ @Override
+ public void onDestroy() {
+ super.onDestroy();
+ NetworkUtils.unregisterNetworkStatusChangedListener(this);
+ dispose();
+ closeConnect();
+ if (mScreenLockReceiver != null) {
+ unregisterReceiver(mScreenLockReceiver);
+ }
+// unbindService(mServiceConnection);
+ }
+
+ private static final String CHANNEL_ID = "CHANNEL_ID";
+ private static final String CHANNEL_NAME = "系统通知";
+ private static final String CHANNEL_DESCRIPTION = "学习课堂通知";
+
+ private void createNotificationChannel() {
+ // Create the NotificationChannel, but only on API 26+ because
+ // the NotificationChannel class is new and not in the support library
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+ CharSequence name = CHANNEL_NAME;
+ String description = CHANNEL_DESCRIPTION;
+ int importance = NotificationManager.IMPORTANCE_DEFAULT;
+ NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
+ channel.setDescription(description);
+ // Register the channel with the system; you can't change the importance
+ // or other notification behaviors after this
+ NotificationManager notificationManager = getSystemService(NotificationManager.class);
+ notificationManager.createNotificationChannel(channel);
+ }
+ }
+
+ private NotificationManagerCompat mNotificationManagerCompat;
+ private int NotificationID = 1;
+
+ private void sendSimpleNotification() {
+ Intent intent = new Intent(this, MainActivity.class);
+ intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
+ PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
+ NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "CHANNEL_ID")
+ .setSmallIcon(R.mipmap.ic_launcher)
+ .setContentTitle("学习课堂正在运行")
+// .setContentText("测试内容")
+ .setAutoCancel(false)
+ .setShowWhen(false)
+ .setContentIntent(pendingIntent)
+ .setOngoing(true)
+ .setOnlyAlertOnce(true)
+ .setPriority(NotificationCompat.PRIORITY_MAX);
+ // notificationId is a unique int for each notification that you must define
+// mNotificationManagerCompat.notify(NotificationID, builder.build());
+ startForeground(NotificationID, builder.build());
+ }
+
+ private ScreenLockReceiver mScreenLockReceiver;
+
+ private void registerScreenLockReceiver() {
+ if (null == mScreenLockReceiver) {
+ mScreenLockReceiver = new ScreenLockReceiver();
+ }
+ IntentFilter filter = new IntentFilter();
+ filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
+ filter.addAction(Intent.ACTION_SCREEN_OFF);
+ filter.addAction(Intent.ACTION_SCREEN_ON);
+ filter.addAction(Intent.ACTION_BOOT_COMPLETED);
+ filter.addAction(Intent.ACTION_USER_PRESENT);
+ filter.addAction(Intent.ACTION_SHUTDOWN);
+ filter.addAction(Intent.ACTION_FACTORY_RESET);
+ filter.addAction(Intent.ACTION_MASTER_CLEAR);
+ registerReceiver(mScreenLockReceiver, filter);
+ }
+
+ private class ScreenLockReceiver extends BroadcastReceiver {
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ String action = intent.getAction();
+ Log.e(TAG, "onReceive:" + action);
+ if (TextUtils.isEmpty(action)) {
+ Log.e(TAG, "onReceive: is NULL");
+ return;
+ }
+ switch (action) {
+ case Intent.ACTION_BOOT_COMPLETED:
+ case Intent.ACTION_USER_PRESENT:
+ case Intent.ACTION_SCREEN_ON:
+ sendMsgScreen();
+ break;
+ case Intent.ACTION_SCREEN_OFF:
+ case Intent.ACTION_SHUTDOWN:
+ case Intent.ACTION_FACTORY_RESET:
+ case Intent.ACTION_MASTER_CLEAR:
+ sendMsgScreen();
+ break;
+ default:
+ break;
+ }
+ }
+ }
+
+ private Disposable mDisposable;
+
+ private void dispose() {
+ if (mDisposable != null && !mDisposable.isDisposed()) {
+ mDisposable.dispose();
+ mDisposable = null;
+ }
+ }
+
+ private void startLoop() {
+ if (mDisposable != null && !mDisposable.isDisposed()) {
+ mDisposable.dispose();
+ mDisposable = null;
+ }
+ mDisposable = Observable.interval(30, TimeUnit.SECONDS)
+ .subscribe(new Consumer