从应用程序类退出应用程序

时间:2016-09-05 21:05:27

标签: android exception-handling

我有一个A类,它扩展了Application.In我正在处理uncaughtexceptions。现在问题是每当应用遇到任何问题应用程序冻结并且在崩溃之前出现黑屏

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);
        }
    });
}

我搜索了很多,但一切都徒劳无功。谢谢。

1 个答案:

答案 0 :(得分:0)

您可以使用以下方式:

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 ();
        ntent.setAction ("com.mydomain.SEND_LOG"); // see step 5.
        intent.setFlags (Intent.FLAG_ACTIVITY_NEW_TASK); // required when starting from Application
        startActivity (intent);
  }
}

根据此answer