优化应用退出与异常销毁流程、项目性能代码优化

This commit is contained in:
844143714@qq,com 2026-04-30 01:30:00 +08:00
parent 828ad240eb
commit 2d7b00be45
6 changed files with 84 additions and 82 deletions

View File

@ -97,7 +97,7 @@ public class CallBackResolution {
case "80"://返回群组信息 case "80"://返回群组信息
Log.d(TAG, "at_OEM_AT_cb get group info: " + lisHex); Log.d(TAG, "at_OEM_AT_cb get group info: " + lisHex);
String strGroup = ""; StringBuilder strGroup = new StringBuilder();
String groupId = lisHex.get(6) + "" + lisHex.get(7) + "" + lisHex.get(8) + "" + lisHex.get(9); String groupId = lisHex.get(6) + "" + lisHex.get(7) + "" + lisHex.get(8) + "" + lisHex.get(9);
int groupNo = Integer.valueOf((lisHex.get(4) + "" + lisHex.get(5)), 16); int groupNo = Integer.valueOf((lisHex.get(4) + "" + lisHex.get(5)), 16);
int groupCount = Integer.valueOf((lisHex.get(10) + "" + lisHex.get(11)), 16); int groupCount = Integer.valueOf((lisHex.get(10) + "" + lisHex.get(11)), 16);
@ -108,9 +108,9 @@ public class CallBackResolution {
size = (size / 2) - 1; size = (size / 2) - 1;
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
if (i == 0) { if (i == 0) {
strGroup += ngx + lisHex.get(i + numB) + "" + lisHex.get(i + numB - 1); strGroup.append(ngx).append(lisHex.get(i + numB)).append(lisHex.get(i + numB - 1));
} else { } else {
strGroup += ngx + lisHex.get(i + numB + 1) + "" + lisHex.get(i + numB); strGroup.append(ngx).append(lisHex.get(i + numB + 1)).append(lisHex.get(i + numB));
numB = numB + 1; numB = numB + 1;
} }
} }
@ -118,7 +118,7 @@ public class CallBackResolution {
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
String tempTxt = ""; String tempTxt = "";
tempTxt = unicodeToString(strGroup); tempTxt = unicodeToString(strGroup.toString());
Log.i(TAG, " 组名称 " + tempTxt); Log.i(TAG, " 组名称 " + tempTxt);
CallBackUtil.callBackGroupInfo(groupId, tempTxt, groupNo, groupCount); CallBackUtil.callBackGroupInfo(groupId, tempTxt, groupNo, groupCount);
Log.d(TAG, "at_OEM_AT_cb group info groupId: " + groupId + ", groupNo: " + groupNo+", groupCount: " +groupCount); Log.d(TAG, "at_OEM_AT_cb group info groupId: " + groupId + ", groupNo: " + groupNo+", groupCount: " +groupCount);
@ -129,7 +129,7 @@ public class CallBackResolution {
break; break;
case "81"://返回群成员信息 case "81"://返回群成员信息
String groupMemberName = ""; StringBuilder groupMemberName = new StringBuilder();
String status = lisHex.get(1); String status = lisHex.get(1);
boolean haveVideo = lisHex.get(10).equals("01") ? true : false; boolean haveVideo = lisHex.get(10).equals("01") ? true : false;
String memberId = lisHex.get(6) + "" + lisHex.get(7) + "" + lisHex.get(8) + "" + lisHex.get(9); String memberId = lisHex.get(6) + "" + lisHex.get(7) + "" + lisHex.get(8) + "" + lisHex.get(9);
@ -141,16 +141,16 @@ public class CallBackResolution {
size = (size / 2) - 1; size = (size / 2) - 1;
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
if (i == 0) { if (i == 0) {
groupMemberName += ngx + lisHex.get(i + numB) + "" + lisHex.get(i + numB - 1); groupMemberName.append(ngx).append(lisHex.get(i + numB)).append(lisHex.get(i + numB - 1));
} else { } else {
groupMemberName += ngx + lisHex.get(i + numB + 1) + "" + lisHex.get(i + numB); groupMemberName.append(ngx).append(lisHex.get(i + numB + 1)).append(lisHex.get(i + numB));
numB = numB + 1; numB = numB + 1;
} }
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
String tempTxt = unicodeToString(groupMemberName); String tempTxt = unicodeToString(groupMemberName.toString());
GroupMemberInfoDto infoDto = new GroupMemberInfoDto(memberId, tempTxt, status, memberNo, haveVideo); GroupMemberInfoDto infoDto = new GroupMemberInfoDto(memberId, tempTxt, status, memberNo, haveVideo);
if (memberNo == 1) memberInfoDtos.clear(); if (memberNo == 1) memberInfoDtos.clear();
memberInfoDtos.add(infoDto); memberInfoDtos.add(infoDto);
@ -461,10 +461,11 @@ public class CallBackResolution {
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
String tempTxt = ""; StringBuilder sb = new StringBuilder();
for (int k = 0; k < strings.size(); k++) { for (int k = 0; k < strings.size(); k++) {
tempTxt += unicode(strings.get(k)); sb.append(unicode(strings.get(k)));
} }
String tempTxt = sb.toString();
Log.i(TAG,"at_Play_TTS_cb " + tempTxt); Log.i(TAG,"at_Play_TTS_cb " + tempTxt);
if (tempTxt.contains("已登录")) { if (tempTxt.contains("已登录")) {
String showName = tempTxt.substring(3, tempTxt.length()); String showName = tempTxt.substring(3, tempTxt.length());

View File

@ -65,9 +65,7 @@ class LoginActivity : AppCompatActivity() {
// 4. 退出按钮 // 4. 退出按钮
btnExit.setOnClickListener { btnExit.setOnClickListener {
fullCleanup() com.stand.standapp.utils.AppKiller.killApp(this)
finishAffinity()
android.os.Process.killProcess(android.os.Process.myPid())
} }
// 5. 彩蛋:点击底部版权 10 次进入开发者模式 // 5. 彩蛋:点击底部版权 10 次进入开发者模式
@ -148,18 +146,17 @@ class LoginActivity : AppCompatActivity() {
} catch (e: Exception) { } catch (e: Exception) {
Timber.tag("PTT").e(e, "PTT init error") Timber.tag("PTT").e(e, "PTT init error")
} }
Thread {
Toast.makeText(this@LoginActivity, "PTT初始化中...", Toast.LENGTH_SHORT).show()
// 使用 Handler 替代硬编码 Thread.sleep避免阻塞或随意开辟线程
android.os.Handler(android.os.Looper.getMainLooper()).postDelayed({
try { try {
Thread.sleep(500)
runOnUiThread {
Toast.makeText(this@LoginActivity, "PTT初始化中", Toast.LENGTH_SHORT).show()
}
Thread.sleep(3000)
com.example.kingway.ptt.SendAtUtil.toLogin("gzzdweb01", "123456", "61.243.1.123", 1, 1) com.example.kingway.ptt.SendAtUtil.toLogin("gzzdweb01", "123456", "61.243.1.123", 1, 1)
} catch (e: Exception) { } catch (e: Exception) {
Timber.tag("PTT").e(e, "PTT login error") Timber.tag("PTT").e(e, "PTT login error")
} }
}.start() }, 1500)
// 2. 启动硬件 GPIO 监听 // 2. 启动硬件 GPIO 监听
try { try {
@ -242,57 +239,9 @@ class LoginActivity : AppCompatActivity() {
.setTitle("提示") .setTitle("提示")
.setMessage("确定要退出应用吗?") .setMessage("确定要退出应用吗?")
.setPositiveButton("确定") { _, _ -> .setPositiveButton("确定") { _, _ ->
fullCleanup() com.stand.standapp.utils.AppKiller.killApp(this)
finishAffinity()
Process.killProcess(Process.myPid())
} }
.setNegativeButton("取消", null) .setNegativeButton("取消", null)
.show() .show()
} }
private fun fullCleanup() {
pttLogoutAndDestroy()
}
private fun pttLogoutAndDestroy() {
try {
com.example.kingway.ptt.SendAtUtil.toCancelLogin()
} catch (e: Exception) {
Timber.tag("LoginActivity").e(e, "PTT cancel failed")
}
try {
com.example.kingway.ptt.SendAtUtil.toLogout()
Timber.tag("LoginActivity").i("PTT logout sent")
} catch (e: Exception) {
Timber.tag("LoginActivity").e(e, "PTT logout failed")
}
try {
com.stand.standapp.utils.GpioManager.stopListening()
} catch (e: Exception) {
Timber.tag("LoginActivity").e(e, "GPIO stop failed")
}
try {
com.stand.standapp.utils.GpioManager.stopListening()
} catch (e: Exception) {
Timber.tag("LoginActivity").e(e, "GPIO stop failed")
}
try {
// MyApplication.destroy()
Timber.tag("LoginActivity").i("PTT native destroyed")
} catch (e: Exception) {
Timber.tag("LoginActivity").e(e, "PTT destroy failed")
}
try {
// PrinterManager.shutdown()
Timber.tag("LoginActivity").i("Printer shut down")
} catch (e: Exception) {
Timber.tag("LoginActivity").e(e, "Printer shutdown failed")
}
try {
com.stand.standapp.utils.LogManager.shutdown()
Timber.tag("LoginActivity").i("LogManager shut down")
} catch (e: Exception) {
Timber.tag("LoginActivity").e(e, "LogManager shutdown failed")
}
}
} }

View File

@ -61,16 +61,7 @@ class MainActivity : AppCompatActivity() {
.setMessage("您的 PTT 账号已在其他设备登录,当前设备已被迫下线。\n请点击确认退出并重新登录。") .setMessage("您的 PTT 账号已在其他设备登录,当前设备已被迫下线。\n请点击确认退出并重新登录。")
.setCancelable(false) .setCancelable(false)
.setPositiveButton("确认") { _, _ -> .setPositiveButton("确认") { _, _ ->
try { com.stand.standapp.utils.AppKiller.killApp(ctx)
com.example.kingway.ptt.SendAtUtil.toLogout()
// com.example.kingway.ptt.MyApplication.destroy()
com.stand.standapp.utils.GpioManager.stopListening()
} catch (_: Exception) {}
val pm = ctx.packageManager
val intent = pm.getLaunchIntentForPackage(ctx.packageName)
intent?.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK)
ctx.startActivity(intent)
android.os.Process.killProcess(android.os.Process.myPid())
} }
.show() .show()
} }
@ -263,9 +254,7 @@ class MainActivity : AppCompatActivity() {
.setTitle("提示") .setTitle("提示")
.setMessage("确定要退出应用吗?") .setMessage("确定要退出应用吗?")
.setPositiveButton("确定") { _, _ -> .setPositiveButton("确定") { _, _ ->
pttLogoutAndDestroy() com.stand.standapp.utils.AppKiller.killApp(this)
finishAffinity()
android.os.Process.killProcess(android.os.Process.myPid())
} }
.setNegativeButton("取消", null) .setNegativeButton("取消", null)
.show() .show()

View File

@ -57,6 +57,9 @@ class MyApplication : Application() {
super.onCreate() super.onCreate()
mApp = this mApp = this
pageName = packageName pageName = packageName
com.stand.standapp.utils.AppKiller.setupUncaughtExceptionHandler(this)
initPTT() initPTT()
// 第一步:初始化日志 // 第一步:初始化日志
LogManager.init(this) LogManager.init(this)

View File

@ -0,0 +1,58 @@
package com.stand.standapp.utils
import android.content.Context
import timber.log.Timber
object AppKiller {
fun killApp(context: Context, isCrash: Boolean = false) {
Timber.tag("AppKiller").i("Starting app cleanup. isCrash: $isCrash")
try {
GpioManager.stopListening()
Timber.tag("AppKiller").i("GPIO stopped")
} catch (e: Exception) {
Timber.tag("AppKiller").e(e, "GPIO stop failed")
}
try {
com.example.kingway.ptt.SendAtUtil.toCancelLogin()
com.example.kingway.ptt.SendAtUtil.toLogout()
Timber.tag("AppKiller").i("PTT logged out")
} catch (e: Exception) {
Timber.tag("AppKiller").e(e, "PTT logout failed")
}
try {
com.stand.standapp.printer.PrinterManager.getFactory()?.ClosePort()
Timber.tag("AppKiller").i("Printer port closed")
} catch (e: Exception) {
Timber.tag("AppKiller").e(e, "Printer close failed")
}
try {
LogManager.shutdown()
} catch (e: Exception) {
// ignore
}
Timber.tag("AppKiller").i("Cleanup finished. Killing process.")
if (!isCrash) {
try {
Thread.sleep(200)
} catch (e: Exception) {}
}
android.os.Process.killProcess(android.os.Process.myPid())
}
fun setupUncaughtExceptionHandler(context: Context) {
val defaultHandler = Thread.getDefaultUncaughtExceptionHandler()
Thread.setDefaultUncaughtExceptionHandler { thread, throwable ->
Timber.tag("AppKiller").e(throwable, "Uncaught exception detected! Force cleanup.")
killApp(context, true)
defaultHandler?.uncaughtException(thread, throwable)
}
}
}

View File

@ -12,6 +12,7 @@ import timber.log.Timber
*/ */
object GpioManager { object GpioManager {
private var mZysjSystemManager: ZysjSystemManager? = null private var mZysjSystemManager: ZysjSystemManager? = null
@Volatile
private var mIsRunning = false private var mIsRunning = false
private var mThread: Thread? = null private var mThread: Thread? = null
private var lastGpioValue = 1 // 默认松开状态 (1) private var lastGpioValue = 1 // 默认松开状态 (1)
@ -54,6 +55,7 @@ object GpioManager {
fun stopListening() { fun stopListening() {
mIsRunning = false mIsRunning = false
mThread?.interrupt()
mThread = null mThread = null
} }