refactor(app): 统一工具类并简化服务架构

- 移除 GuardService、StepService、ControlPanelService,替换为 SocketService
- 将 QR 码生成从 Utils 迁移到 BitmapUtils
- 将缓存目录选择逻辑统一到 FileUtils.getCacheFileDir
- 将 ServiceAliveUtils、NetStateUtils 合并到 SystemUtils
- 将 CacheUtils 同步清理替换为 ApkUtils.cleanApplicationUserDataAsync
- 用 EncryptUtils 替代自定义 MD5Util
- 重写 catchException 支持主线程兜底循环与子线程 Bugly 上报
- 移除 RunningAppManager 中未使用的 killBackgroundProcesses 方法
- 移除 ControlManager 中冗余的 hideSystemSettingAPP 调用
- MyGlideModule 重命名为 BaseGlideModule
This commit is contained in:
tongtong committed 2026-11-07 17:55:56 +08:00
1 parent df8a7bc153
commit a972ce7b89
46 files changed
+1583 -1812

No files matched your search

@@ -24,11 +24,9 @@ import com.fuying.sn.manager.DeviceManager;
import com.fuying.sn.manager.time.TimeManager;
import com.fuying.sn.push.PushManager;
import com.fuying.sn.rlog.LogDBManager;
import com.fuying.sn.service.ControlPanelService;
import com.fuying.sn.service.DownloadService;
import com.fuying.sn.service.GuardService;
import com.fuying.sn.service.ManagerService;
import com.fuying.sn.service.StepService;
import com.fuying.sn.service.SocketService;
import com.fuying.sn.service.main.MainService;
import com.fuying.sn.sync.AppInventorySync;
import com.fuying.sn.sync.AppPolicySync;
@@ -51,6 +49,15 @@ import com.tencent.mmkv.MMKV;
public class BaseApplication extends Application {
private static final String TAG = "BaseApplication";
/**
* 兜底循环内允许的连续主线程异常次数,超过后交回默认处理器重启进程
*/
private static final int MAX_CONTINUOUS_CRASH = 5;
/**
* 两次主线程异常超过该间隔(毫秒)就重新计数,避免偶发异常被累计成连续崩溃
*/
private static final long CRASH_RESET_INTERVAL = 30_000L;
/**
* ViewModel中因为经常旋转导致弱引用为空
*/
@@ -68,7 +75,6 @@ public class BaseApplication extends Application {
//非主进程不初始化
if (SystemUtils.isMainProcessName(this, android.os.Process.myPid())) {
if (BuildConfig.DEBUG) {
// ApkUtils.openApp(this, "com.android.settings");
//静音
// AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
// int ringMax = audioManager.getStreamMinVolumeInt(AudioManager.STREAM_RING);
@@ -87,9 +93,6 @@ public class BaseApplication extends Application {
}
private void utilsInint() {
if (!BuildConfig.DEBUG) {
catchException();
}
String rootDir = MMKV.initialize(this);
Log.i(TAG, "mmkv root: " + rootDir);
@@ -100,6 +103,10 @@ public class BaseApplication extends Application {
JgyUtils.init(this);
CrashReport.initCrashReport(getApplicationContext(), "e5f026c8d5", false);
CrashReport.setDeviceId(this, Utils.getSerial());
if (!BuildConfig.DEBUG) {
// Bugly 先接管默认异常处理器,兜底处理器才能把子线程异常继续传递给它完成上报
catchException();
}
LogDBManager.init(this);
Aria.init(this);
@@ -141,40 +148,80 @@ public class BaseApplication extends Application {
private void startService() {
startService(new Intent(this, MainService.class));
startService(new Intent(this, GuardService.class));
startService(new Intent(this, StepService.class));
startService(new Intent(this, SocketService.class));
startService(new Intent(this, DownloadService.class));
startService(new Intent(this, ManagerService.class));
startService(new Intent(this, ControlPanelService.class));
}
/**
* 线上崩溃兜底:主线程单次异常只记录并继续分发消息,避免常驻进程被一次异常打断;
* 子线程异常继续交给原有处理器(Bugly),保证崩溃能正常上报。
*/
private void catchException() {
Thread.setDefaultUncaughtExceptionHandler(
new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
Log.e("捕获异常子线程:", Thread.currentThread().getName() +
"在:" + e.getStackTrace()[0].getClassName());
}
final Thread.UncaughtExceptionHandler originHandler =
Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
logCrash("子线程", t, e);
if (originHandler != null) {
// 不吞掉异常:默认处理器里是 Bugly 的上报与系统退出逻辑
originHandler.uncaughtException(t, e);
} else {
Log.e(TAG, "catchException: 默认异常处理器为空,子线程崩溃无法上报");
}
);
//下面是新增方法!
}
});
// 主线程消息分发的异常会冒泡出 Looper.loop() 直接结束进程,用嵌套循环拦下来单独记录
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
int crashCount = 0;
long lastCrashTime = 0L;
while (true) {
try {
Looper.loop(); //会先执行这个方法,然后在执行下面的异常捕获方法!
} catch (Exception e) {
Log.e("捕获异常主线程:", Thread.currentThread().getName() +
"在:" + e.getStackTrace()[0].getClassName());
e.printStackTrace();
Looper.loop(); // 接管主线程消息分发,消息里抛出的异常会落到下面的 catch
} catch (Throwable e) {
long now = System.currentTimeMillis();
if (now - lastCrashTime > CRASH_RESET_INTERVAL) {
crashCount = 0;
}
lastCrashTime = now;
crashCount++;
logCrash("主线程", Thread.currentThread(), e);
if (e instanceof Error || crashCount >= MAX_CONTINUOUS_CRASH) {
// Error 不可恢复、或短时间内反复崩溃说明进程状态已不可信,
// 交回默认处理器让系统按 START_STICKY 重新拉起,不再继续兜底
Log.e(TAG, "catchException: 主线程连续异常次数=" + crashCount);
if (originHandler != null) {
originHandler.uncaughtException(Thread.currentThread(), e);
return;
}
throw new RuntimeException("默认异常处理器为空", e);
}
reportCatchedException(e);
}
}
}
});
}
private void logCrash(String source, Thread thread, Throwable e) {
// 直接取 getStackTrace()[0] 在堆栈为空时会在异常处理里二次崩溃,这里取完整堆栈
Log.e(TAG, "logCrash: " + source + " thread=" + thread.getName()
+ " 在:" + Log.getStackTraceString(e));
}
private void reportCatchedException(Throwable e) {
try {
// 被兜底拦下的异常不会经过 Bugly 的 UncaughtExceptionHandler,必须主动上报
CrashReport.postCatchedException(e);
} catch (Throwable reportError) {
Log.e(TAG, "reportCatchedException: 崩溃上报失败 "
+ Log.getStackTraceString(reportError));
}
}
private void aliyunPushInit() {
// PushInitConfig pushInitConfig = new PushInitConfig.Builder()
// .application(this)