Can I call a method before my application go to crash

前端 未结 5 1270
庸人自扰
庸人自扰 2020-12-30 16:26

I\'m a newbie in android and I always see Exception when I\'m running my code. So, somebody can tell me Can I call a method before app go to crash anywhere without \"try-cat

相关标签:
5条回答
  • 2020-12-30 16:43

    This would be better way to handle uncaught exception:

    public class MyApplication extends Application {
    @Override
        public void onCreate() {
            super.onCreate();
            appInitialization();
        }
    
        private void appInitialization() {
             defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
             Thread.setDefaultUncaughtExceptionHandler(_unCaughtExceptionHandler);
        }
    
        private UncaughtExceptionHandler defaultUEH;
    
        // handler listener
        private Thread.UncaughtExceptionHandler _unCaughtExceptionHandler = new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread thread, Throwable ex) {
                ex.printStackTrace();
                // TODO handle exception here
            }
        };
    }
    

    Application is a Base class for those who need to maintain global application state. And hence here, it will be a better place to handle such exceptions.

    EDIT: The above code will handle uncaught exceptions if they are thrown inside UI thread. If an exception has occurred in worker thread, you can handle it in following way:

    private boolean isUIThread(){
            return Looper.getMainLooper().getThread() == Thread.currentThread();
        }
    // 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
    
            if (isUIThread()) {
                // exception occurred from UI thread
                invokeSomeActivity();
    
            } else {  //handle non UI thread throw uncaught exception
    
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        invokeSomeActivity();
                    }
                });
            }
        }
    
    0 讨论(0)
  • 2020-12-30 16:52

    I think what you search is the UncaughtExceptionHandler. It is notified whenever an Exception is fired and not catched on its way bubbling up through your application.

    See http://www.intertech.com/Blog/android-handling-the-unexpected/ for more details on implementing this in android.

    0 讨论(0)
  • 2020-12-30 16:52


    There is method called Uncaught exception which is called just before force close dialog , you can write ur piece of code there .. Please check Using Global Exception Handling on android

    0 讨论(0)
  • 2020-12-30 17:01

    Use CrashLytics for crash reporter free of cost and easy to implement

    https://www.crashlytics.com/

    0 讨论(0)
  • 2020-12-30 17:06

    Try this way

    1) create class

    import android.content.Context;
    import android.content.Intent;
    import android.os.Process;
    
    import java.io.PrintWriter;
    import java.io.StringWriter;
    import java.lang.Thread.UncaughtExceptionHandler;
    
    public class CrashReportHandler implements UncaughtExceptionHandler {
    
        public static void attach(Context context) {
            Thread.setDefaultUncaughtExceptionHandler(
                    new CrashReportHandler(context)
            );
        }
    
        ///////////////////////////////////////////// implementation
    
        private CrashReportHandler(Context context) {
            m_context = context;
        }
    
        public void uncaughtException(Thread thread, Throwable exception) {
            StringWriter stackTrace = new StringWriter();
            exception.printStackTrace(new PrintWriter(stackTrace));
    
            //You will get call back here when app crashes.
    
            // from RuntimeInit.crash()
            Process.killProcess(Process.myPid());
            System.exit(10);
        }
    
        private Context m_context;
    
    }
    

    How to use this class?

    write this line in your activity onCreate() method

    CrashReportHandler.attach(this);
    
    0 讨论(0)
提交回复
热议问题