Android每次运行时保存日志以用于崩溃报告

11
我目前正在开发一款安卓应用程序。我注意到一个非常罕见的错误,导致我的应用程序崩溃。不幸的是,当它发生时,我的智能手机从未连接到我的电脑上。那么,是否有一种方法可以在我的应用程序启动时自动保存所有日志(特别是抛出的运行时异常)到一个文件中,以便我可以将此文件复制到我的电脑上并分析该错误?该文件应该在每次启动我的应用程序时被覆盖,这样它只包含最后一次运行的日志... 我该如何实现这一点?
敬礼
1个回答

19

您可以通过以下链接获得帮助:将崩溃报告写入设备SD卡中

您无需添加外部库。

import com.wordpress.doandroid.Training.R;

import android.app.Activity;
import android.os.Bundle;

public class CaptureExceptionActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Sets the default uncaught exception handler. This handler is invoked
        // in case any Thread dies due to an unhandled exception.
        Thread.setDefaultUncaughtExceptionHandler(new CustomizedExceptionHandler(
            "/mnt/sdcard/"));

        String nullString = null;
        System.out.println(nullString.toString());
        setContentView(R.layout.main);
    }
}

还有Handler的实现

import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.Thread.UncaughtExceptionHandler;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.os.Environment;
import android.util.Log;

public class CustomizedExceptionHandler implements UncaughtExceptionHandler {

    private UncaughtExceptionHandler defaultUEH;
    private String localPath;
    public CustomizedExceptionHandler(String localPath) {
        this.localPath = localPath;
        //Getting the the default exception handler
        //that's executed when uncaught exception terminates a thread
        this.defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
    }

    public void uncaughtException(Thread t, Throwable e) {

        //Write a printable representation of this Throwable
        //The StringWriter gives the lock used to synchronize access to this writer.
        final Writer stringBuffSync = new StringWriter();
        final PrintWriter printWriter = new PrintWriter(stringBuffSync);
        e.printStackTrace(printWriter);
        String stacktrace = stringBuffSync.toString();
        printWriter.close();

        if (localPath != null) {
            writeToFile(stacktrace);
        }

        //Used only to prevent from any code getting executed.
        // Not needed in this example
        defaultUEH.uncaughtException(t, e);
    }

    private void writeToFile(String currentStacktrace) {
        try {

            //Gets the Android external storage directory & Create new folder Crash_Reports
            File dir = new File(Environment.getExternalStorageDirectory(),
                "Crash_Reports");
            if (!dir.exists()) {
                dir.mkdirs();
            }

            SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyy_MM_dd_HH_mm_ss");
            Date date = new Date();
            String filename = dateFormat.format(date) + ".STACKTRACE";

            // Write the file into the folder
            File reportFile = new File(dir, filename);
            FileWriter fileWriter = new FileWriter(reportFile);
            fileWriter.append(currentStacktrace);
            fileWriter.flush();
            fileWriter.close();
        } catch (Exception e) {
            Log.e("ExceptionHandler", e.getMessage());
        }
    }

}

不要忘记在清单中添加此权限:WRITE_EXTERNAL_STORAGE

太棒了!但是defaultUEH是什么,它只用于防止执行任何代码,并且在这个例子中不需要?那么它在哪里需要呢? - Acuna
你可以将这个语句作为注释放置,以了解其效果。通常情况下,如果没有这个语句,你的应用程序不会崩溃。但是如果有了这个语句,它会崩溃,并且你会在崩溃前得到堆栈跟踪信息。 - Hasina

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接