【问题标题】:Catch Or Detect ANR Exception捕获或检测 ANR 异常
【发布时间】:2012-10-11 01:11:02
【问题描述】:

如果应用程序崩溃了,我想发送 /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);
    }

【问题讨论】:

    标签: android crash detect


    【解决方案1】:

    一种可靠的方法是使用“脏位”;当您的应用程序启动时向文件写入一个值,并在它干净关闭时向文件写入一个不同的值。每次你的应用程序启动时,在写入之前检查文件中的内容;如果不是你在应用干净关闭时写入的值,你就知道应用崩溃了。

    【讨论】:

      【解决方案2】:

      您也可以使用 ACRA http://code.google.com/p/acra/。它将使您能够将崩溃报告直接发送到您的应用服务器/第三方服务器。

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-11
      • 1970-01-01
      • 2010-12-14
      • 2011-05-01
      • 1970-01-01
      • 2020-05-13
      • 1970-01-01
      • 2021-01-24
      相关资源
      最近更新 更多