捕获或检测ANR异常

the*_*r0w 2 crash android detect

我想发送/data/anr/traces.txt,如果应用程序崩溃了,但问题是我怎么知道我的应用程序崩溃了.

    File file = new File("/data/anr/traces.txt");
    if (file.exists()) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.putExtra("subject", file.getName());
        intent.putExtra("body", "...");
        intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));

        if (file.getName().endsWith(".gz")) {
            intent.setType("application/x-gzip");
        } else if (file.getName().endsWith(".txt")) {
            intent.setType("text/plain");
        } else {
            intent.setType("application/octet-stream");
        }

                    // ? Can I send it without permission?
        startActivity(intent);
    }
Run Code Online (Sandbox Code Playgroud)

Kel*_*vin 6

一种可靠的方法是使用"脏位"; 在应用程序启动时将值写入文件,并在文件干净地关闭时为文件写入不同的值.每次你的应用程序启动时,在写入之前检查文件中的内容; 如果它不是你在应用程序干净地关闭时写的价值,你知道应用程序崩溃了.