0

我有一个带有全局异常处理程序的应用程序,如下所示:

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));

    }
}

我想避免显示力靠近用户并向用户显示友好的祝酒词,例如“出了点问题......”。这是异常处理程序类:

public class ExceptionHandler implements
        Thread.UncaughtExceptionHandler {
    private final Context myContext;
    private final String LINE_SEPARATOR = "\n";


    public ExceptionHandler(Context context) {
        myContext = context;
    }

    public void uncaughtException(Thread thread, Throwable exception) {
        StringWriter stackTrace = new StringWriter();
        exception.printStackTrace(new PrintWriter(stackTrace));
        StringBuilder errorReport = new StringBuilder();
        errorReport.append("************ CAUSE OF ERROR ************\n\n");
        errorReport.append(stackTrace.toString());
        Log.e("ERROR_TAG", errorReport.toString());
        Utils.showShortToast(R.string.something_went_wrong, myContext);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                   android.os.Process.killProcess(android.os.Process.myPid());
                   System.exit(10);
            }
        },1000);
    }
}

问题是在显示 toast 时,应用程序冻结并等到 system.exit() 被调用,然后应用程序退出。正如它在下面的参考问题中注意到的那样,在显示 toast 后立即调用 exit() 会导致进程终止并且不显示 toast。

PS 我读过这个这个这个,但没有一个能找到解决方案。

4

1 回答 1

-1

在 onDestroy() 中制作吐司。我认为这是一个好方法:)

最好的问候

于 2016-08-10T13:08:40.330 回答