简体   繁体   English

我想以编程方式在Android中获取应用程序崩溃信息,而无需使用第三方应用程序或jars(例如bugsense等)

[英]I want to get application crash info in Android programmatically without using 3rd party apps or jars like bugsense etc

I have a webview based android application I want to get the crash report and store it into a text file but i am unable to find any programmatic native solution anywhere. 我有一个基于webview的android应用程序,我想获取崩溃报告并将其存储到文本文件中,但是我无法在任何地方找到任何编程的本机解决方案。 I have read this article but I am unable to use it. 我已经阅读了这篇文章,但无法使用。 I have no code because I dont know where to start 我没有代码,因为我不知道从哪里开始

The best way to handle crash logs is creating an UncaughtExceptionHandler and handling it as per your requirement. 处理崩溃日志的最佳方法是创建一个UncaughtExceptionHandler并根据您的要求进行处理。 Create a BaseActivity class and extend all the Activities with that and put this code stuff in the BaseActivity class. 创建一个BaseActivity类,并使用该类扩展所有Activity,并将此代码内容放入BaseActivity类中。

private Thread.UncaughtExceptionHandler handleAppCrash = 
                                     new Thread.UncaughtExceptionHandler() {
    @Override
    public void uncaughtException(Thread thread, Throwable ex) {
        Log.e("error", ex.toString());
        //send email here
    }
};

Then just enable is inside onCreate() method of your BaseActivity by using 然后通过使用在您的BaseActivity的onCreate()方法内部启用

Thread.setDefaultUncaughtExceptionHandler(handleAppCrash);

I am catching un-handled exceptions by using this in my activity's onCreate(): 我通过在活动的onCreate()中使用此方法来捕获未处理的异常:

mUEHandler = new Thread.UncaughtExceptionHandler() {

        @Override
        public void uncaughtException(Thread t, Throwable e) {
            try {

                PrintWriter pw = new PrintWriter(new OutputStreamWriter(
                        openFileOutput(DMP_FILENAME, 0)));
                e.printStackTrace(pw);
                pw.flush();
                pw.close();
            } catch (FileNotFoundException e1) {
                // do nothing
            }
            BaseActivity.this.finish();
        }
    };

    Thread.setDefaultUncaughtExceptionHandler(mUEHandler);

This writes every unhandled exception in your app that happened on your activity to text file. 这会将您活动中发生的所有未处理的异常都写入文本文件。 Then you can analyze it. 然后,您可以对其进行分析。

为什么不记录Log.e(“ tag”,“ msg”)之类的所有内容,否则检查logcat的崩溃情况

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM