gpt4 book ai didi

android - 当 Activity 从崩溃中重新启动时捕获

转载 作者:行者123 更新时间:2023-11-30 02:54:05 25 4
gpt4 key购买 nike

您好,我正在处理一个有时有问题并导致 Activity 重启的第 3 方库。有没有办法判断 Activity 何时从崩溃中重新启动?我试过像这样使用未捕获的异常处理程序,但它没有被触发。

Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable throwable) {
Log.d("un", "caught");
}
});

最佳答案

这样写

Thread.setDefaultUncaughtExceptionHandler(new MyExceptionHandler(this,YOURCURRENTCLASSNAME.class));

亲爱的,请使用这个类(class)。我也用过这个

public class MyExceptionHandler implements
java.lang.Thread.UncaughtExceptionHandler {
private final Context myContext;
private final Class<?> myActivityClass;

public MyExceptionHandler(Context context, Class<?> c) {

myContext = context;
myActivityClass = c;
}

public void uncaughtException(Thread thread, Throwable exception) {

StringWriter stackTrace = new StringWriter();
exception.printStackTrace(new PrintWriter(stackTrace));
System.err.println(stackTrace);// You can use LogCat too
Intent intent = new Intent(myContext, myActivityClass);
String s = stackTrace.toString();
// you can use this String to know what caused the exception and in
// which Activity
intent.putExtra("uncaughtException",
"Exception is: " + stackTrace.toString());
intent.putExtra("stacktrace", s);
myContext.startActivity(intent);
// for restarting the Activity
// Process.killProcess(Process.myPid());
System.out.println("comingggggggggggggggggg in crashhhhhhhhhhhhhhhhhhhh and restrttttttttttttt autometically ");
Intent i = myContext.getPackageManager().getLaunchIntentForPackage(myContext.getPackageName() );
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addCategory(Intent.CATEGORY_HOME);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
myContext.startActivity(i);
System.exit(0);
}
}

关于android - 当 Activity 从崩溃中重新启动时捕获,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23623495/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com