【问题标题】:Exit app from application class从应用程序类中退出应用程序
【发布时间】:2017-01-13 05:51:08
【问题描述】:

我有一个扩展应用程序的 A 类。在 A 中,我正在处理未捕获的异常。现在的问题是每当应用程序遇到任何问题时,应用程序都会冻结并在崩溃前出现黑屏

public class A extends Application {
  @Override
  public void onCreate() {
    super.onCreate();
    final AppContext context = AppContext.getInstance();
    context.setContext(this);
    Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
        @Override
        public void uncaughtException(Thread thread, Throwable e) {
            mContext = context.getContext();
            e.getCause().getMessage();
            AppPreference.getInstance().setCrashReason(e.getMessage());
            Intent intent = new Intent ();
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
            startActivity (intent);
            System.exit(1);
        }
    });
}

我已经搜索了很多,但都是徒劳的。提前谢谢。

【问题讨论】:

    标签: android exception-handling


    【解决方案1】:

    您可以使用以下方式:

    public class MyApplication extends Application
    {
      public void onCreate ()
      {
        // Setup handler for uncaught exceptions.
        Thread.setDefaultUncaughtExceptionHandler (new Thread.UncaughtExceptionHandler()
        {
          @Override
          public void uncaughtException (Thread thread, Throwable e)
          {
            handleUncaughtException (thread, e);
          }
        });
      }
    
    public void handleUncaughtException (Thread thread, Throwable e)
      {
        e.printStackTrace(); // not all Android versions will print the stack trace automatically
           
            Intent intent = new Intent ();
            intent.setAction ("com.mydomain.SEND_LOG"); 
            intent.setFlags (Intent.FLAG_ACTIVITY_NEW_TASK); // required when starting from Application
            startActivity (intent);
      }
    }
    

    据此answer

    【讨论】:

    • 这就是我现在正在做的事情。应用程序冻结并出现黑屏。一段时间后应用程序无响应警报出现。
    • @ashlok malik 检查提供的链接以获得完整答案。
    • 我已经参考了上面提到的链接,但是应用程序仍然被冻结,一旦遇到任何错误就会显示黑屏
    猜你喜欢
    • 2022-01-20
    • 2014-01-18
    • 2011-02-09
    • 2015-01-18
    • 1970-01-01
    • 2014-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多