[Android Notes] crash log collection and Android crash logs

Source: Internet
Author: User

[Android Notes] crash log collection and Android crash logs

After the application was released, some people reported that a crash occurred, but I was unable to locate the problem because I couldn't get the log. Later we found that we should collect crash logs and upload them to the server.

There are a lot of third-party institutions in China to provide the crash collection sdk, we can use it directly, for example, the app I previously made uses the service provided by bugHD (http://bughd.com.

But what is the principle of crash collection? After searching, we found that the uncaughtExceptionHandler in java is used. We can set our own UncaughtExceptionHandler through Thread. setDefautUncaughtExceptionHandler. It is actually an interface, implementation method.


A demo I wrote:

Import android. OS. logoff; import android. widget. toast; import java. io. file; import java. io. fileOutputStream; import java. io. IOException; import java. io. printWriter; import java. io. stringWriter; import java. io. writer; import java. text. simpleDateFormat; import java. util. date; import java. util. locale; import java. util. map;/*** Created by Rowandjj on 2015/5/19. ** crash log Collection */public class CrashHandler implements T Hread. uncaughtExceptionHandler {private static CrashHandler sInstance = null; private static Object lock = new Object (); private Thread. uncaughtExceptionHandler mDefaultExceptionHandler; private CrashHandler () {} public static CrashHandler getInstance () {if (sInstance = null) {synchronized (lock) {if (sInstance = null) sInstance = new CrashHandler () ;}return sInstance;} public void regis Ter () {mDefaultExceptionHandler = Thread. getDefaultUncaughtExceptionHandler (); Thread. setDefaultUncaughtExceptionHandler (this) ;}@ Override public void uncaughtException (Thread thread, Throwable ex) {// generate the log final String filename = cacheLog (ex); new Thread (new Runnable () {@ Override public void run () {logoff. prepare (); if (filename! = Null) Toast. makeText (AppEnv. getAppContext (), "The program crashed: (\ n the crash log has been saved to" + filename + "", Toast. LENGTH_SHORT ). show (); logoff. loop ();}}). start (); try {Thread. sleep (2000);} catch (InterruptedException e) {e. printStackTrace ();} mDefaultExceptionHandler. uncaughtException (thread, ex);} private String cacheLog (Throwable ex) {if (ex = null) return null; Map <String, String> hardwareInfo = HardwareUtils. getHard WareInfo (); StringBuilder buffer = new StringBuilder (); for (Map. entry <String, String> me: hardwareInfo. entrySet () {buffer. append (me. getKey () + ":" + me. getValue () + "\ n");} buffer. append ("packname:" + AppEnv. getPackageName () + "\ n"); buffer. append ("versionname:" + AppEnv. getVersionName () + "\ n"); buffer. append ("versioncode:" + AppEnv. getVersionCode () + "\ n"); Writer writer = new StringWriter (); PrintWriter printWriter = new PrintWriter (writer); ex. printStackTrace (printWriter); Throwable cause = ex. getCause (); while (cause! = Null) {cause. printStackTrace (printWriter); cause = cause. getCause ();} printWriter. close (); String result = writer. toString (); buffer. append (result); long timestamp = System. currentTimeMillis (); SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd-HH-mm-ss", Locale. CHINA); String time = formatter. format (new Date (); String filename = "crash-" + time + "-" + timestamp + ". log "; File file = New File (BasicUtils. getStoreDir (), filename); try {if (! File. exists () file. createNewFile (); FileOutputStream out = new FileOutputStream (file); out. write (buffer. toString (). getBytes (); out. close (); Logger. d (this, file. getAbsolutePath (); return file. getAbsolutePath ();} catch (IOException e) {e. printStackTrace ();} return null ;}}

Then, call

CrashHandler.getInstance().register();
You can.

When a crash occurs in an application, the log is saved to the local device. If you want to upload data to the server, add an http upload module.


Some of the Code is not posted, so you can make up your own brain.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.