{"id":2640,"date":"2016-04-23T18:00:06","date_gmt":"2016-04-23T18:00:06","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=2640"},"modified":"2016-04-23T18:00:06","modified_gmt":"2016-04-23T18:00:06","slug":"how-to-send-app-crash-reports-in-android","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2016\/04\/23\/how-to-send-app-crash-reports-in-android\/","title":{"rendered":"How to send app crash reports in Android"},"content":{"rendered":"            <script type=\"text\/javascript\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/plugins\/wordpress-code-snippet\/scripts\/shBrushJava.js\"><\/script>\n<p>As a developer you often need to know the details of an app crash, specially if it happens on a customer&#8217;s device or any remote device. If your app is being downloaded via Play Store then you can use the existing Google services to track your crash reports. But if your app is still being tested or its a private app meant for restricted usage, then you cannot depend on Play Store to handle it for you.<\/p>\n<p>There are quite a few commercial as well as free services like ACRA, Crashlytics etc. But sometimes, you dont need a full fledged service or API to do crash handling. At the very basic level, all a developer needs is the stack trace of the exception that crashed the app.<\/p>\n<p>You can easily roll out your own module for handling a crash and gathering the details. This code traps any unhandled exception within your app, captures the detailed stack trace and allows a user to send the details as an email to a designated email id.<\/p>\n<p>The core logic is simple &#8211; we subclass the main Application class and trap any unhandled exception, get the stack trace and call a Mailing Intent. That is all. We dont need to change any existing code anywhere else.<\/p>\n<p>Create a Class file and extend it from Application.<br \/>\n<pre class=\"brush: java\">package com.myapp.test;\r\n\r\n\r\nimport android.app.Application;\r\nimport android.content.Context;\r\nimport android.content.Intent;\r\nimport android.util.Log;\r\n\r\n\r\n\/**\r\n * Created by amit on 22\/4\/16.\r\n *\/\r\npublic class TestApplication extends Application {\r\n\r\n    @Override\r\n    public void onCreate() {\r\n        super.onCreate();\r\n        Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {\r\n\r\n            @Override\r\n            public void uncaughtException(Thread thread, Throwable ex) {\r\n                handleUncaughtException(thread, ex);\r\n\r\n            }\r\n        });\r\n    }\r\n\r\n    public void handleUncaughtException (Thread thread, Throwable e)\r\n    {\r\n        String stackTrace = Log.getStackTraceString(e);\r\n        String message = e.getMessage();\r\n\r\n\r\n        Intent intent = new Intent (Intent.ACTION_SEND);\r\n        intent.setType(&quot;message\/rfc822&quot;);\r\n        intent.putExtra (Intent.EXTRA_EMAIL, new String[] {&quot;myemail@email.com&quot;});\r\n        intent.putExtra (Intent.EXTRA_SUBJECT, &quot;MyApp Crash log file&quot;);\r\n        intent.putExtra (Intent.EXTRA_TEXT, stackTrace);\r\n        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); \/\/ required when starting from Application\r\n        startActivity(intent);\r\n\r\n        \r\n    }\r\n\r\n\r\n\r\n}\r\n<\/pre><br \/>\nIn your AndroidManifest.xml specify the name for your application tag to be the name of your Class file.<\/p>\n<pre>&lt;application\r\n    android:name=\".TestApplication\"<\/pre>\n<p>Whenever your application crashes, it will get caught in your custom Application class, and then it will call the default\u00a0 mailing application in the device by setting up a Mail Intent. Everything in the mail will already be filled &#8211; the user just has to click on Send. Or he can even copy the mail content and save it somewhere.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>As a developer you often need to know the details of an app crash, specially if it happens on a customer&#8217;s device or any remote <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2016\/04\/23\/how-to-send-app-crash-reports-in-android\/\" title=\"How to send app crash reports in Android\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":2643,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[295],"tags":[],"class_list":["post-2640","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android-dev"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2640","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=2640"}],"version-history":[{"count":3,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2640\/revisions"}],"predecessor-version":[{"id":2644,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2640\/revisions\/2644"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/2643"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=2640"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=2640"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=2640"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}