[8.1.0][fmd] 修复handler崩溃,修复dialog崩溃

This commit is contained in:
xinfengkun
2025-06-19 15:40:25 +08:00
parent 43dd627986
commit 370bb87367
4 changed files with 28 additions and 113 deletions

View File

@@ -1,93 +0,0 @@
package com.zhjt.mogo_core_function_devatools.rviz.common.base;
import android.app.Service;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import java.lang.ref.WeakReference;
public abstract class BaseService extends Service {
private BaseHandler mBaseHandler;
/**
* 初始化一个Handler如果需要使用Handler先调用此方法
* 然后可以使用postRunnable(Runnable runnable)
* sendMessage在handleMessageMessage msg中接收msg
*/
protected void initHandler() {
initHandler(true);
}
protected void initHandler(boolean isMain) {
if (isMain) {
mBaseHandler = new BaseHandler(this, Looper.getMainLooper());
} else {
new Thread() {
@Override
public void run() {
super.run();
Looper.prepare(); // 创建Looper
mBaseHandler = new BaseHandler(BaseService.this, Looper.myLooper());
Looper.loop(); // 启动Looper的消息循环
}
}.start();
}
}
/**
* 返回Handler在此之前确定已经调用initHandler
*
* @return Handler
*/
protected Handler getHandler() {
return mBaseHandler;
}
/**
* 同Handler 的 handleMessage
* getHandler.sendMessage,发送的Message在此接收
* 在此之前确定已经调用initHandler
*
* @param msg
*/
protected void handleMessage(Message msg) {
}
/**
* 同Handler的postRunnable
* 在此之前确定已经调用initHandler
*/
protected void postRunnable(Runnable runnable) {
postRunnableDelayed(runnable, 0);
}
/**
* 同Handler的postRunnableDelayed
* 在此之前确定已经调用initHandler
*/
protected void postRunnableDelayed(Runnable runnable, long delayMillis) {
if (mBaseHandler == null) initHandler();
mBaseHandler.postDelayed(runnable, delayMillis);
}
protected static class BaseHandler extends Handler {
private final WeakReference<BaseService> mObjects;
public BaseHandler(BaseService mPresenter, Looper looper) {
super(looper);
mObjects = new WeakReference<BaseService>(mPresenter);
}
@Override
public void handleMessage(Message msg) {
BaseService mPresenter = mObjects.get();
if (mPresenter != null) mPresenter.handleMessage(msg);
}
}
}

View File

@@ -34,13 +34,12 @@ class InputUserPwdDialog(
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val rootView = layoutInflater.inflate(R.layout.rviz_fmd_dialog_fmd_user_pwd, null, false)
setContentView(R.layout.rviz_fmd_dialog_fmd_user_pwd)
val titleView: TextView = findViewById(R.id.title)
val inputRosUserName: EditText = findViewById(R.id.input_ros_user_name)
val inputRosPassword: EditText = findViewById(R.id.input_ros_password)
inputRosUserName = findViewById(R.id.input_ros_user_name)
inputRosPassword= findViewById(R.id.input_ros_password)
val btnCancel: Button = findViewById(R.id.btnCancel)
val btnConfirm: Button = findViewById(R.id.btnConfirm)
setContentView(rootView)
window?.setBackgroundDrawable(null)
titleView.text = host.hostname
inputRosUserName.setOnEditorActionListener(onEditorActionListener)

View File

@@ -1,14 +1,13 @@
package com.zhjt.mogo_core_function_devatools.rviz.service
import android.content.Context
import android.app.Service
import android.content.Intent
import android.os.Binder
import android.os.Build
import android.os.Handler
import android.os.HandlerThread
import android.os.IBinder
import android.os.Looper
import android.os.Message
import android.os.VibrationEffect
import android.os.Vibrator
import android.os.VibratorManager
import android.text.TextUtils
import android.util.Log
import android.util.Pair
@@ -18,19 +17,15 @@ import com.mogo.eagle.core.function.api.autopilot.IMoGoAutopilotCarConfigListene
import com.mogo.eagle.core.function.api.autopilot.IMoGoAutopilotStatusListener
import com.mogo.eagle.core.function.api.autopilot.IMoGoFaultManagementStateListener
import com.mogo.eagle.core.function.call.autopilot.CallerAutoPilotStatusListenerManager
import com.mogo.eagle.core.function.call.autopilot.CallerAutopilotActionsListenerManager
import com.mogo.eagle.core.function.call.autopilot.CallerAutopilotCarConfigListenerManager
import com.mogo.eagle.core.function.call.autopilot.CallerFaultManagementStateListenerManager
import com.mogo.eagle.core.utilcode.util.GsonUtils
import com.mogo.eagle.core.utilcode.util.ToastUtils
import com.zhjt.mogo.adas.data.AdasConstants
import com.zhjt.mogo_core_function_devatools.rviz.R
import com.zhjt.mogo_core_function_devatools.rviz.common.base.BaseService
import com.zhjt.mogo_core_function_devatools.rviz.common.config.SSHAccountConfig
import com.zhjt.mogo_core_function_devatools.rviz.common.coroutines.FlowBus
import com.zhjt.mogo_core_function_devatools.rviz.common.utils.Utils
import com.zhjt.mogo_core_function_devatools.rviz.constant.AppConfigInfo
import com.zhjt.mogo_core_function_devatools.rviz.constant.DiagnoseSource
import com.zhjt.mogo_core_function_devatools.rviz.constant.DiagnoseType
import com.zhjt.mogo_core_function_devatools.rviz.constant.EventKey
import com.zhjt.mogo_core_function_devatools.rviz.constant.FaultLevel
@@ -67,8 +62,8 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import mogo.telematics.pad.MessagePad
import java.lang.ref.WeakReference
import java.util.Locale
import java.util.Timer
import java.util.TimerTask
@@ -78,7 +73,7 @@ import java.util.concurrent.atomic.AtomicReference
/**
* 故障管理诊断服务
*/
class FaultManagementDiagnosisService : BaseService(), OnSshConnectionListener,
class FaultManagementDiagnosisService : Service(), OnSshConnectionListener,
IMoGoAutopilotCarConfigListener,
IMoGoFaultManagementStateListener, OnExecCommandListener,
OnDockerExecCommandListener {
@@ -136,7 +131,6 @@ class FaultManagementDiagnosisService : BaseService(), OnSshConnectionListener,
override fun onCreate() {
super.onCreate()
initHandler(false)//初始化了个子线程Handler
initFMData()
CallerAutoPilotStatusListenerManager.addListener(TAG, adasConnectionStatuslistener)
CallerAutopilotCarConfigListenerManager.addListener(TAG, this)
@@ -146,7 +140,6 @@ class FaultManagementDiagnosisService : BaseService(), OnSshConnectionListener,
override fun onBind(intent: Intent?): IBinder {
initHandler(false)//初始化了个子线程Handler
Log.i(TAG, "故障管理诊断服务已绑定")
return binder
}
@@ -163,7 +156,7 @@ class FaultManagementDiagnosisService : BaseService(), OnSshConnectionListener,
override fun onDestroy() {
super.onDestroy()
handler.removeCallbacksAndMessages(null)
CallerFaultManagementStateListenerManager.removeListener(TAG)
CallerAutopilotCarConfigListenerManager.removeListener(TAG)
CallerAutoPilotStatusListenerManager.removeListener(TAG)
@@ -313,8 +306,7 @@ class FaultManagementDiagnosisService : BaseService(), OnSshConnectionListener,
}
}
override fun handleMessage(msg: Message) {
super.handleMessage(msg)
private fun handleMessage(msg: Message) {
when (msg.what) {
WHAT_FM_INFO_TIMEOUT -> {
//接收消息超时域控没有发FM数据认为是无异常
@@ -1202,4 +1194,21 @@ class FaultManagementDiagnosisService : BaseService(), OnSshConnectionListener,
}
}
// 子线程 Handler线程安全地延迟初始化
private val handler: Handler by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
val handlerThread = HandlerThread("FMDThread").apply { start() }
ChildThreadHandler(this, handlerThread.looper)
}
/**
* 子线程 Handler持有 service 的弱引用防止内存泄漏
*/
private class ChildThreadHandler(service: FaultManagementDiagnosisService, looper: Looper) :
Handler(looper) {
private val serviceRef = WeakReference(service)
override fun handleMessage(msg: Message) {
serviceRef.get()?.handleMessage(msg)
}
}
}