weizhengliang пре 6 месеци
родитељ
комит
5157cff7a0

+ 18 - 38
android_mobile/src/main/yd_w_xiaomi_2_chile/code/com/wdkl/ncs/android/component/home/activity/WatchHome2Activity.kt

@@ -123,14 +123,9 @@ class WatchHome2Activity : BaseActivity<WatchHomeActivityPresenter, WatchActivit
 
         initCountDownTimer()
 
-        if (DaemonEnv.app == null) {
+        /*if (DaemonEnv.app == null) {
             DaemonEnv.init(this)
-        }
-
-        if (!WdKeepAliveService.instanceCreated) {
-            Log.d(TAG, "开始 WdKeepAliveService")
-            DaemonEnv.startServiceSafely(this, WdKeepAliveService::class.java)
-        }
+        }*/
 
         //网络强度监听
         teleManager = (applicationContext.getSystemService(Context.TELEPHONY_SERVICE)) as TelephonyManager
@@ -422,9 +417,9 @@ class WatchHome2Activity : BaseActivity<WatchHomeActivityPresenter, WatchActivit
             loadingDialog.dismiss()
             showMessage(R.string.device_not_register)
             tv_register_status.setText(R.string.device_not_register_tips)
-            if (DaemonEnv.app != null) {
+            /*if (DaemonEnv.app != null) {
                 SpManager.getInstance().putBoolean(Constants.SYSTEM_REGISTERED, false)
-            }
+            }*/
             tv_register_ok.visibility = View.VISIBLE
             watch_activity_register_layout.visibility = View.VISIBLE
             watch_activity_home_linyout.visibility = View.GONE
@@ -467,14 +462,17 @@ class WatchHome2Activity : BaseActivity<WatchHomeActivityPresenter, WatchActivit
             return
         }
 
-        if (DaemonEnv.app != null) {
+        /*if (DaemonEnv.app != null) {
             SpManager.getInstance().putBoolean(Constants.SYSTEM_REGISTERED, true)
+        }*/
+
+        if (!WdKeepAliveService.instanceCreated) {
+            Log.d(TAG, "开始 WdKeepAliveService")
+            DaemonEnv.startServiceSafely(this, WdKeepAliveService::class.java)
         }
 
         presenter.getDeviceSettingData(Constants.partId)
 
-        //onTcpConnectSuccess()
-
         if (Strings.isNullOrEmpty(Constants.userName)) {
             watch_name_tv.setText(R.string.str_reload)
             watch_name_tv.setTextColor(Color.BLACK)
@@ -603,37 +601,19 @@ class WatchHome2Activity : BaseActivity<WatchHomeActivityPresenter, WatchActivit
     fun onTcpConnectSuccess() {
         Log.i(TAG, "连接成功")
 
-        runOnUiThread(Runnable {
-            Log.i(TAG, "连接成功设置UI")
-            tv_net_reconnect_text.visibility = View.GONE
-            tv_signal_strength.setText(R.string.tcp_connect_success)
-            netOffLoadingDialog.dismiss()
-            sip_state_tv.setBackgroundColor(Color.parseColor("#00FFFF"))
-        })
+        tv_net_reconnect_text.visibility = View.GONE
+        tv_signal_strength.setText(R.string.tcp_connect_success)
+        netOffLoadingDialog.dismiss()
+        sip_state_tv.setBackgroundColor(Color.parseColor("#00FFFF"))
     }
 
     fun onTcpConnectFailed() {
         Log.w(TAG, "网络断开")
 
-        runOnUiThread(Runnable {
-            tv_signal_strength.setText(R.string.tcp_connect_failed)
-            tv_net_reconnect_text.visibility = View.VISIBLE
-            netOffLoadingDialog.show()
-            sip_state_tv.setBackgroundColor(Color.parseColor("#FF0000"))
-        })
-
-        //等30秒
-        var waitSeconds = 0
-        Thread {
-            while (!TcpClientHandler.getConnected()) {
-                Log.w(TAG, "无网络" + waitSeconds)
-                waitSeconds++
-                if (waitSeconds > 30) {
-                    break
-                }
-                Thread.sleep(5000)
-            }
-        }.start()
+        tv_signal_strength.setText(R.string.tcp_connect_failed)
+        tv_net_reconnect_text.visibility = View.VISIBLE
+        netOffLoadingDialog.show()
+        sip_state_tv.setBackgroundColor(Color.parseColor("#FF0000"))
     }
 
     override fun bindEvent() {

+ 20 - 40
android_mobile/src/main/yd_watch_2/code/com/wdkl/ncs/android/component/home/activity/WatchHome2Activity.kt

@@ -133,14 +133,9 @@ class WatchHome2Activity : BaseActivity<WatchHomeActivityPresenter, WatchActivit
 
         initCountDownTimer()
 
-        if (DaemonEnv.app == null) {
+        /*if (DaemonEnv.app == null) {
             DaemonEnv.init(this)
-        }
-
-        if (!WdKeepAliveService.instanceCreated) {
-            Log.d(TAG, "开始 WdKeepAliveService")
-            DaemonEnv.startServiceSafely(this, WdKeepAliveService::class.java)
-        }
+        }*/
 
         if (SettingConfig.getSipEnabled(BaseApplication.appContext)) {
             Thread {
@@ -541,9 +536,9 @@ class WatchHome2Activity : BaseActivity<WatchHomeActivityPresenter, WatchActivit
             loadingDialog.dismiss()
             showMessage("本机未注册,请将识别码发给管理员")
             tv_register_status.text = "本机未注册,请将识别码发给管理员。管理员注册本机后请点击注册完成进入"
-            if (DaemonEnv.app != null) {
+            /*if (DaemonEnv.app != null) {
                 SpManager.getInstance().putBoolean(Constants.SYSTEM_REGISTERED, false)
-            }
+            }*/
             tv_register_ok.visibility = View.VISIBLE
             watch_activity_register_layout.visibility = View.VISIBLE
             watch_activity_home_linyout.visibility = View.GONE
@@ -590,14 +585,17 @@ class WatchHome2Activity : BaseActivity<WatchHomeActivityPresenter, WatchActivit
             return
         }
 
-        if (DaemonEnv.app != null) {
+        /*if (DaemonEnv.app != null) {
             SpManager.getInstance().putBoolean(Constants.SYSTEM_REGISTERED, true)
+        }*/
+
+        if (!WdKeepAliveService.instanceCreated) {
+            Log.e(TAG, "开始 WdKeepAliveService")
+            DaemonEnv.startServiceSafely(this, WdKeepAliveService::class.java)
         }
 
         presenter.getDeviceSettingData(Constants.partId)
 
-        //onTcpConnectSuccess()
-
         if (Strings.isNullOrEmpty(Constants.userName)) {
             watch_name_tv.setText(R.string.str_reload)
             watch_name_tv.setTextColor(Color.BLACK)
@@ -727,37 +725,19 @@ class WatchHome2Activity : BaseActivity<WatchHomeActivityPresenter, WatchActivit
     fun onTcpConnectSuccess() {
         Log.i(TAG, "连接成功")
 
-        runOnUiThread(Runnable {
-            Log.i(TAG, "连接成功设置UI")
-            tv_net_reconnect_text.visibility = View.GONE
-            tv_signal_strength.text = "网络正常"
-            netOffLoadingDialog.dismiss()
-            sip_state_tv.setBackgroundColor(Color.parseColor("#00FFFF"))
-        })
+        tv_net_reconnect_text.visibility = View.GONE
+        tv_signal_strength.text = "网络正常"
+        netOffLoadingDialog.dismiss()
+        sip_state_tv.setBackgroundColor(Color.parseColor("#00FFFF"))
     }
 
     fun onTcpConnectFailed() {
         Log.w(TAG, "网络断开")
 
-        runOnUiThread(Runnable {
-            tv_signal_strength.text = "网络断开"
-            tv_net_reconnect_text.visibility = View.VISIBLE
-            netOffLoadingDialog.show()
-            sip_state_tv.setBackgroundColor(Color.parseColor("#FF0000"))
-        })
-
-        //等30秒
-        var waitSeconds = 0;
-        Thread {
-            while (!TcpClientHandler.getConnected()) {
-                Log.w(TAG, "无网络" + waitSeconds)
-                waitSeconds++
-                if (waitSeconds > 30) {
-                    break
-                }
-                Thread.sleep(5000)
-            }
-        }.start()
+        tv_signal_strength.text = "网络断开"
+        tv_net_reconnect_text.visibility = View.VISIBLE
+        netOffLoadingDialog.show()
+        sip_state_tv.setBackgroundColor(Color.parseColor("#FF0000"))
     }
 
     override fun bindEvent() {
@@ -937,7 +917,7 @@ class WatchHome2Activity : BaseActivity<WatchHomeActivityPresenter, WatchActivit
         }*/
     }
 
-    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
+    /*override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
         //Log.e(TAG, "onKeyDown 1111111 keyCode: $keyCode, keyEvent: $event")
         //ACTION_SEND_SOS
         //android.intent.action.sgtc_SOSaction
@@ -955,7 +935,7 @@ class WatchHome2Activity : BaseActivity<WatchHomeActivityPresenter, WatchActivit
         //Log.e(TAG, "onKeyUp 33333333333 keyCode: $keyCode, keyEvent: $event")
         longPressed = false
         return super.onKeyUp(keyCode, event)
-    }
+    }*/
 
     override fun onError(message: String, type: Int) {
     }

+ 19 - 2
android_mobile/src/main/yd_watch_2/code/com/wdkl/ncs/android/component/home/service/WdKeepAliveService.kt

@@ -89,7 +89,15 @@ class WdKeepAliveService : AbsWorkService() {
     private var warningTips = false
     private val keepHandler: Handler = object : Handler(Looper.getMainLooper()) {
         override fun handleMessage(msg: Message) {
-            warningTips = false
+            if (msg.what == 0) {
+                warningTips = false
+            } else if (msg.what == 1) {
+                if (!TcpClientHandler.getConnected()) {
+                    //1分钟后还是未连接上服务器,直接重启app
+                    Util.wakeUpAndUnlock()
+                    AppUtils.restartApp()
+                }
+            }
         }
     }
 
@@ -731,9 +739,18 @@ class WdKeepAliveService : AbsWorkService() {
 
             //网络断开
             Constants.EVENT_TCP_BREAK -> {
-                Log.w(TAG, "EVENT TCP BREAK")
+                Log.e(TAG, "EVENT TCP BREAK")
                 //MediaPlayHelper.getInstance().playResMusic(R.raw.tcp_disconnect, 1.0f, false)
                 //RingPlayHelper.playRingTone(BaseApplication.appContext, R.raw.tcp_disconnect, false)
+
+                keepHandler.removeMessages(1)
+                keepHandler.sendEmptyMessageDelayed(1, 60000)
+            }
+
+            Constants.EVENT_RESTART_APP -> {
+                //app重启
+                Util.wakeUpAndUnlock()
+                AppUtils.restartApp()
             }
 
             //留言播放完成

+ 1 - 0
app/build.gradle

@@ -135,6 +135,7 @@ dependencies {
 
     implementation project(':middleware')
     implementation project(':android_mobile')
+    implementation project(':keepalive')
 }
 
 ///**

+ 5 - 2
app/src/main/code/com/wdkl/app/ncs/application/Application.kt

@@ -1,6 +1,5 @@
 package com.wdkl.app.ncs.application
 
-//import android.content.Context
 import android.content.Context
 import android.content.res.Configuration
 import com.enation.javashop.android.jrouter.JRouter
@@ -11,7 +10,7 @@ import com.hjq.toast.Toaster
 import com.wdkl.ncs.android.component.home.settingconfig.SettingConfig
 import com.wdkl.ncs.android.component.home.util.LocaleMangerUtils
 import com.wdkl.ncs.android.lib.base.BaseApplication
-//import me.weishu.reflection.Reflection
+import com.wdkl.ncs.keepbackground.work.DaemonEnv
 
 /**
  * @author LDD
@@ -76,6 +75,10 @@ class Application : BaseApplication() {
                 .addNetInterceptor(RestfulExceptionInterceptor())
 
         Toaster.init(this)
+
+        if (DaemonEnv.app == null) {
+            DaemonEnv.init(this)
+        }
     }
 
     override fun attachBaseContext(base: Context?) {

+ 2 - 1
keepalive/src/main/java/com/wdkl/ncs/keepbackground/watch/WatchDogService.java

@@ -52,12 +52,13 @@ public class WatchDogService extends Service {
     @Override
     public void onCreate() {
         super.onCreate();
+        ForegroundNotificationUtils.startForegroundNotification(this);
+
         isCanStartWatchDog = WatchProcessPrefHelper.getIsStartDaemon(this);
         if (!isCanStartWatchDog){
             stopSelf();
         }
         startRegisterReceiver();
-        ForegroundNotificationUtils.startForegroundNotification(this);
     }
 
     @Override

+ 1 - 1
keepalive/src/main/java/com/wdkl/ncs/keepbackground/watch/WatchProcessPrefHelper.java

@@ -52,7 +52,7 @@ public class WatchProcessPrefHelper {
     }
 
     public static boolean getIsStartDaemon(Context context){
-        return context.getSharedPreferences(SHARED_UTILS, Context.MODE_MULTI_PROCESS).getBoolean(KEY_IS_START_DAEMON, true);
+        return context.getSharedPreferences(SHARED_UTILS, Context.MODE_MULTI_PROCESS).getBoolean(KEY_IS_START_DAEMON, false);
     }