2012-10-11 104 views
1

我想發送/data/anr/traces.txt如果應用程序曾經崩潰,但問題是我怎麼知道我的應用程序崩潰之前。抓住或檢測ANR異常

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); 
    } 

回答

1

或者,您可以使用ACRA http://code.google.com/p/acra/。它將使您能夠將崩潰報告直接發送到您的應用服務器/第三方服務器。

+0

順便說一句,ACRA崩潰報告與traces.txt相同嗎? – thecr0w

+0

@ thecr0w:此鏈接 - https://github.com/SalomonBrys/ANR-WatchDog,也有助於檢測Android應用程序中的ANR – Basher51

5

一個可靠的方法是使用「髒位」;在應用程序啓動時爲文件寫入值,並在文件關閉時向文件寫入不同的值。每次您的應用程序啓動時,請在寫入文件之前檢查文件中的內容;如果它不是您在應用程序完全關閉時編寫的值,則會知道該應用程序崩潰。

+0

thx,我會考慮一下。 – thecr0w