[BuglyFix]修正FinalizerWatchdogDaemon线程后台运行时报TimeOutException
This commit is contained in:
@@ -124,6 +124,9 @@ ext {
|
||||
androidxroomruntime : "androidx.room:room-runtime:2.2.3",
|
||||
androidxroomcompiler : "androidx.room:room-compiler:2.2.3",
|
||||
androidxroomktx : "androidx.room:room-ktx:2.2.3",
|
||||
|
||||
// androidx-lifecycle-process
|
||||
androidxlifecycleprocess : "androidx.lifecycle:lifecycle-process:2.4.0",
|
||||
// rxjava2 with room
|
||||
roomRxjava : 'androidx.room:room-rxjava2:2.2.3',
|
||||
circleimageview : "de.hdodenhof:circleimageview:3.0.1",
|
||||
|
||||
@@ -43,6 +43,7 @@ dependencies {
|
||||
implementation rootProject.ext.dependencies.rxjava
|
||||
api rootProject.ext.dependencies.mogoaicloudrealtime
|
||||
implementation rootProject.ext.dependencies.amapnavi3dmap
|
||||
implementation rootProject.ext.dependencies.androidxlifecycleprocess
|
||||
|
||||
if (Boolean.valueOf(USE_MAVEN_PACKAGE)) {
|
||||
implementation rootProject.ext.dependencies.mogoutils
|
||||
|
||||
@@ -16,6 +16,7 @@ import com.alibaba.android.arouter.launcher.ARouter;
|
||||
import com.mogo.aicloud.services.httpdns.IMogoHttpDns;
|
||||
import com.mogo.aicloud.services.httpdns.MogoHttpDnsHandler;
|
||||
import com.mogo.commons.analytics.AnalyticsUtils;
|
||||
import com.mogo.commons.crash.FinalizeCrashFixer;
|
||||
import com.mogo.commons.debug.DebugConfig;
|
||||
import com.mogo.commons.device.Devices;
|
||||
import com.mogo.commons.network.AllAllowedHostnameVerifier;
|
||||
@@ -59,6 +60,7 @@ public abstract class AbsMogoApplication extends Application {
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
sApp = this;
|
||||
FinalizeCrashFixer.fix();
|
||||
initARouter();
|
||||
Utils.init(this);
|
||||
if (shouldInit()) {
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.mogo.commons.crash;
|
||||
|
||||
import androidx.lifecycle.Lifecycle;
|
||||
import androidx.lifecycle.LifecycleEventObserver;
|
||||
import androidx.lifecycle.ProcessLifecycleOwner;
|
||||
import com.mogo.utils.logger.Logger;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class FinalizeCrashFixer {
|
||||
|
||||
private static void realFix() {
|
||||
try {
|
||||
Class<?> clazz = Class.forName("java.lang.Daemons$FinalizerWatchdogDaemon");
|
||||
if (clazz.getSuperclass() != null) {
|
||||
Field instance = clazz.getDeclaredField("INSTANCE");
|
||||
instance.setAccessible(true);
|
||||
Method isRunningMethod = clazz.getSuperclass().getDeclaredMethod("isRunning");
|
||||
isRunningMethod.setAccessible(true);
|
||||
boolean running = (boolean) isRunningMethod.invoke(instance.get(null));
|
||||
if (running) {
|
||||
// 只有正在执行的情况下才去调用 stop,避免无意义的调用导致产生 Throwable 异常
|
||||
Method stopMethod = clazz.getSuperclass().getDeclaredMethod("stop");
|
||||
stopMethod.setAccessible(true);
|
||||
stopMethod.invoke(instance.get(null));
|
||||
}
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 只有线程在运行状态才会关闭线程
|
||||
*/
|
||||
public static void fix() {
|
||||
ProcessLifecycleOwner.get().getLifecycle().addObserver((LifecycleEventObserver) (source, event) -> {
|
||||
if (event == Lifecycle.Event.ON_START) {
|
||||
//前后台则重新反射关闭一遍,避免线程被再次开启
|
||||
Logger.d("FinalizeCrashFixer", "--- 切换到前台 ---");
|
||||
realFix();
|
||||
}
|
||||
|
||||
if (event == Lifecycle.Event.ON_STOP) {
|
||||
//前后台则重新反射关闭一遍,避免线程被再次开启
|
||||
Logger.d("FinalizeCrashFixer", "--- 切换到后台 ---");
|
||||
realFix();
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user