当应用程序因未处理的异常而崩溃时显示错误页面
Show error page when application crash because of unhandled exception
我想在我的应用程序因某些未处理的异常而崩溃时启动 activity。我已经实施了以下内容
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
handelUnCoughtException(t,e);
}
});
}
private void handelUnCoughtException(Thread t , Throwable e){
Toast.makeText(this , "Handeling Error" , Toast.LENGTH_LONG).show();
Intent serviceintent = new Intent(this , ErrorHandelingService.class);
startService(serviceintent);
}
}
以上是我的应用程序 class,我在其中设置了默认的 setDefaultUncaughtExceptionHandler 但是我无法在我有代码启动的地方启动我的服务 activity 请帮忙。
我为未捕获的异常写了以下内容class
public class UncaughtExceptionHandler 实现 Thread.UncaughtExceptionHandler {
private Context context;
private Class<? extends AppCompatActivity> erroractivity;
public UncaughtExceptionHandler(Context context, Class<? extends AppCompatActivity> erroractivity) {
this.context = context;
this.erroractivity = erroractivity;
}
public static void setUpErrorHandlingActivity(Context context, Class<? extends AppCompatActivity> erroractivity) {
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler(context, erroractivity));
}
@Override
public void uncaughtException(Thread thread, Throwable throwable) {
new SystemLog().writeAppRecLog("UNCAUGHT_EXCEPTION", "UnCaught Exception.", (Exception) throwable);
Intent errorActivityIntent = new Intent(context, erroractivity);
errorActivityIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
errorActivityIntent.setAction("FROM_ERROR_ACTIVITY");
context.startActivity(errorActivityIntent);
Process.killProcess(Process.myPid());
System.exit(10);
}
}
并且在我的 activity 中我已经注册了这个 class 所以我可以启动我自己的错误 activity
UncaughtExceptionHandler.setUpErrorHandlingActivity(getActivityContext(), ErrorActivity.class);
我想在我的应用程序因某些未处理的异常而崩溃时启动 activity。我已经实施了以下内容
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
handelUnCoughtException(t,e);
}
});
}
private void handelUnCoughtException(Thread t , Throwable e){
Toast.makeText(this , "Handeling Error" , Toast.LENGTH_LONG).show();
Intent serviceintent = new Intent(this , ErrorHandelingService.class);
startService(serviceintent);
}
}
以上是我的应用程序 class,我在其中设置了默认的 setDefaultUncaughtExceptionHandler 但是我无法在我有代码启动的地方启动我的服务 activity 请帮忙。
我为未捕获的异常写了以下内容class
public class UncaughtExceptionHandler 实现 Thread.UncaughtExceptionHandler {
private Context context;
private Class<? extends AppCompatActivity> erroractivity;
public UncaughtExceptionHandler(Context context, Class<? extends AppCompatActivity> erroractivity) {
this.context = context;
this.erroractivity = erroractivity;
}
public static void setUpErrorHandlingActivity(Context context, Class<? extends AppCompatActivity> erroractivity) {
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler(context, erroractivity));
}
@Override
public void uncaughtException(Thread thread, Throwable throwable) {
new SystemLog().writeAppRecLog("UNCAUGHT_EXCEPTION", "UnCaught Exception.", (Exception) throwable);
Intent errorActivityIntent = new Intent(context, erroractivity);
errorActivityIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
errorActivityIntent.setAction("FROM_ERROR_ACTIVITY");
context.startActivity(errorActivityIntent);
Process.killProcess(Process.myPid());
System.exit(10);
}
}
并且在我的 activity 中我已经注册了这个 class 所以我可以启动我自己的错误 activity
UncaughtExceptionHandler.setUpErrorHandlingActivity(getActivityContext(), ErrorActivity.class);