Browse Source

1.3.12 optimize:其他优化

xwh 6 months ago
parent
commit
d3b8ab31cd
1 changed files with 93 additions and 80 deletions
  1. 93 80
      app/src/main/java/com/hh/hhomc/services/BackgroundService.java

+ 93 - 80
app/src/main/java/com/hh/hhomc/services/BackgroundService.java

@@ -581,14 +581,19 @@ public class BackgroundService extends Service {
     @Override
     public void onDestroy() {
         // 在服务销毁时执行一些清理操作
-
-
+        if (mHandler != null) {
+            mHandler.removeCallbacksAndMessages(null);
+            mHandler = null;
+        }
+        isWebsocketConnect=false;
+        if (mWebSocketClient != null) {
+            mWebSocketClient.close();//取消掉以前的长连接
+            mWebSocketClient = null;
+        }
         // 解绑广播
         if (isRegisterDownload && receiver != null) {
             unregisterReceiver(receiver);
         }
-
-
         //解除注册
         if (null != iAidlInterface && iAidlInterface.asBinder().isBinderAlive()) {
             try {
@@ -601,19 +606,12 @@ public class BackgroundService extends Service {
         if (isServiceConnection && serviceConnection != null) {
             unbindService(serviceConnection);
         }
-
         super.onDestroy();
-
-        if (mHandler != null) {
-            mHandler.removeCallbacksAndMessages(null);
-            mHandler = null;
-        }
-
     }
 
 
     URI uri;
-    WebSocketClient mWebSocketClient;
+    volatile static WebSocketClient mWebSocketClient;
 
     Gson gson = new Gson();
 
@@ -630,111 +628,117 @@ public class BackgroundService extends Service {
             e.printStackTrace();
         }
         if (null == mWebSocketClient) {
-            mWebSocketClient = new WebSocketClient(uri) {
-                @Override
-                public void onOpen(ServerHandshake serverHandshake) {
-                    AppLogUtils.e("onOpen: ");
-                    isWebsocketConnect = true;
+            synchronized (BackgroundService.class){
+                if(mWebSocketClient==null){
+                    mWebSocketClient = new WebSocketClient(uri) {
+                        @Override
+                        public void onOpen(ServerHandshake serverHandshake) {
+                            AppLogUtils.e("onOpen: ");
+                            isWebsocketConnect = true;
 
-                    uploadDeviceStatus();
+                            uploadDeviceStatus();
 
 //                    queryServerAppInfo();
 
 //                    isWebsocketConnect = false;
 
-                }
+                        }
 
-                @Override
-                public void onMessage(String serverResultStr) {
-                    AppLogUtils.e("onMessage: " + serverResultStr);
+                        @Override
+                        public void onMessage(String serverResultStr) {
+                            AppLogUtils.e("onMessage: " + serverResultStr);
 
-                    if (StringUtils.isNull(serverResultStr)) {
-                        AppLogUtils.e("onMessage数据空");
-                        return;
-                    }
+                            if (StringUtils.isNull(serverResultStr)) {
+                                AppLogUtils.e("onMessage数据空");
+                                return;
+                            }
 
-                    // 转换服务端返回的数据
-                    ServerActionBean serverActionBean = gson.fromJson(serverResultStr, ServerActionBean.class);
-                    AppLogUtils.e("serverActionBean--" + serverActionBean.toString());
-
-                    if (serverActionBean != null) {
-                        String serverSn = serverActionBean.getSn();
-                        AppLogUtils.i("local-sn--" + sn);
-                        AppLogUtils.i("server-sn--" + serverSn);
-                        if (!sn.equals(serverSn)) {
-                            // 如果sn号不相等,就不操作
-                            AppLogUtils.i("sn号不相等--");
-                            return;
-                        }
+                            // 转换服务端返回的数据
+                            ServerActionBean serverActionBean = gson.fromJson(serverResultStr, ServerActionBean.class);
+                            AppLogUtils.e("serverActionBean--" + serverActionBean.toString());
+
+                            if (serverActionBean != null) {
+                                String serverSn = serverActionBean.getSn();
+                                AppLogUtils.i("local-sn--" + sn);
+                                AppLogUtils.i("server-sn--" + serverSn);
+                                if (!sn.equals(serverSn)) {
+                                    // 如果sn号不相等,就不操作
+                                    AppLogUtils.i("sn号不相等--");
+                                    return;
+                                }
 
-                        AppLogUtils.i("sn号相等--接着执行");
+                                AppLogUtils.i("sn号相等--接着执行");
 
-                        // 服务端要干的操作
-                        String op = serverActionBean.getOp();
-                        AppLogUtils.e("op--" + op);
-                        if (FETCH_LOG.equals(op)) {
-                            AppLogUtils.e(FETCH_LOG + "  拉取日志列表,上传日志文件名列表供展示下载");
+                                // 服务端要干的操作
+                                String op = serverActionBean.getOp();
+                                AppLogUtils.e("op--" + op);
+                                if (FETCH_LOG.equals(op)) {
+                                    AppLogUtils.e(FETCH_LOG + "  拉取日志列表,上传日志文件名列表供展示下载");
 
-                            returnLogNameList(serverActionBean);
+                                    returnLogNameList(serverActionBean);
 
-                        } else if (DOWNLOAD_LOG.equals(op)) {
-                            AppLogUtils.e(DOWNLOAD_LOG + "  后台返回选中要下载日志列表,进行日志文件上传");
+                                } else if (DOWNLOAD_LOG.equals(op)) {
+                                    AppLogUtils.e(DOWNLOAD_LOG + "  后台返回选中要下载日志列表,进行日志文件上传");
 
-                            uploadLogFileList(serverActionBean);
+                                    uploadLogFileList(serverActionBean);
 
-                        } else if (INSTALL_UPGRADE.equals(op)) {
-                            AppLogUtils.e(INSTALL_UPGRADE + "  apk安装升级");
+                                } else if (INSTALL_UPGRADE.equals(op)) {
+                                    AppLogUtils.e(INSTALL_UPGRADE + "  apk安装升级");
 
-                            installApkByServer(serverResultStr);
+                                    installApkByServer(serverResultStr);
 
 
-                        } else if (CANCEL_UPGRADE.equals(op)) {
-                            AppLogUtils.e(CANCEL_UPGRADE + "  apk取消安装升级");
+                                } else if (CANCEL_UPGRADE.equals(op)) {
+                                    AppLogUtils.e(CANCEL_UPGRADE + "  apk取消安装升级");
 
-                            cancelApkByServer(serverResultStr);
+                                    cancelApkByServer(serverResultStr);
 
 
-                        } else if (CLOSE_APP.equals(op)) {
-                            AppLogUtils.e(CLOSE_APP + "  应⽤关闭");
+                                } else if (CLOSE_APP.equals(op)) {
+                                    AppLogUtils.e(CLOSE_APP + "  应⽤关闭");
 
-                            closeAppByServer(serverActionBean);
+                                    closeAppByServer(serverActionBean);
 
-                        } else if (REBOOT.equals(op)) {
-                            AppLogUtils.e(REBOOT + "  应⽤设备重启");
+                                } else if (REBOOT.equals(op)) {
+                                    AppLogUtils.e(REBOOT + "  应⽤设备重启");
 
-                            restartDevice();
+                                    restartDevice();
 
-                        } else if (QUERY_DEVICE_BIZ_DATA.equals(op)) {
-                            AppLogUtils.e(REBOOT + "  获取设备业务参数");
+                                } else if (QUERY_DEVICE_BIZ_DATA.equals(op)) {
+                                    AppLogUtils.e(REBOOT + "  获取设备业务参数");
 
-                            queryDeviceAppInfo(serverResultStr);
+                                    queryDeviceAppInfo(serverResultStr);
 
-                        }
+                                }
 
-                    }
+                            }
 
 
-                }
+                        }
 
-                @Override
-                public void onClose(int i, String s, boolean b) {
-                    // 检查在OTA平台设备列表是否已经添加设备
-                    AppLogUtils.e("onClose: ");
-                    isWebsocketConnect = false;
-                }
+                        @Override
+                        public void onClose(int i, String s, boolean b) {
+                            // 检查在OTA平台设备列表是否已经添加设备
+                            AppLogUtils.e("onClose: ");
+                            isWebsocketConnect = false;
+                        }
 
-                @Override
-                public void onError(Exception e) {
-                    AppLogUtils.e("onError--e = " + e.toString());
-                    // java.net.UnknownHostException: hhomc.hz-hanghui.com
-                    isWebsocketConnect = false;
+                        @Override
+                        public void onError(Exception e) {
+                            AppLogUtils.e("onError--e = " + e.toString());
+                            // java.net.UnknownHostException: hhomc.hz-hanghui.com
+                            isWebsocketConnect = false;
+                        }
+                    };
                 }
-            };
+            }
+
             mWebSocketClient.connect();
 
 //            if (heartBeatRunnable != null) {
 //                mHandler.removeCallbacks(heartBeatRunnable);
 //            }
+            getmHandler().removeMessages(WHAT_HEART);
             getmHandler().sendEmptyMessageDelayed(WHAT_HEART,5 * 1000);//开启心跳检测
 
 
@@ -1066,12 +1070,21 @@ public class BackgroundService extends Service {
 
     private void restartWebsocket() {
         getmHandler().removeMessages(WHAT_HEART);
+        if(mWebSocketClient!=null&&mWebSocketClient.isConnecting()){
+            AppLogUtils.e("restartWebsocket-   mWebSocketClient.Connecting(): ");
+            return;
+        }
         if (mWebSocketClient != null) {
             mWebSocketClient.close();//取消掉以前的长连接
             mWebSocketClient = null;
         }
+        getmHandler().postDelayed(new Runnable() {
+            @Override
+            public void run() {
+                new InitSocketThread().start();//创建一个新的连接
+            }
+        },2000);
 
-        new InitSocketThread().start();//创建一个新的连接
     }
 
     class InitSocketThread extends Thread {