Android will crash information on how to save the application to a local file and upload the server

Source: Internet
Author: User

Lead: Recently really is too busy, no how to update the public number, nor how seriously to write some content, here first to pay attention to my friend said sorry, may be in the next period of time, or very busy, but I will strive to share a bit more technical articles, for everyone to see, common progress, Also hope that the ability of people can come out to share.

When we are doing application development, we need the program crash information, to make bug fixes and version updates, every application will have bugs, so we need to record these bug logs in the background, and then upload to the server, let the programmer see, and repair. Now there are a lot of third-party jar package can realize this function, such as Friends of the league statistics, but after all, not as convenient to write their own. Well, the nonsense is not much to say, now is the lecture.

First step: First look at two classes application and Uncaughtexceptionhandler

Application: Used to manage the global state of an application. When the application starts, application is created first, and then the activity and service are started as appropriate. For processors that do not catch exceptions, we can implement them here as well.

Uncaughtexceptionhandler: The thread did not catch the exception handler to handle the uncaught exception. If the program has an uncaught exception, the dialog box is forced to close on the system by default. We need to implement this interface and register the exception handling as not caught by default in the program. This allows you to do some personalized exception handling when an uncaught exception occurs.

Step two: Thread does not catch the implementation of the exception interface

Import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.ioexception;import Java.io.printstream;import Java.lang.Thread.UncaughtExceptionHandler;         public class Crashhandler implements uncaughtexceptionhandler{private static Crashhandler Crashhandler;        @Override public void uncaughtexception (thread thread, Throwable ex) {//TODO auto-generated method stub if (Crashhandler! = null) {try {//writes crash log to file FileOutputStream fileoutputst                Ream = new FileOutputStream ("/mnt/sdcard/crash_log.txt", true);                PrintStream PrintStream = new PrintStream (FileOutputStream);                Ex.printstacktrace (PrintStream);                Printstream.flush ();                Printstream.close ();            Fileoutputstream.close ();            } catch (FileNotFoundException e) {//TODO auto-generated catch block E.printstacktrace (); } catch (IOExceptione) {//TODO auto-generated catch block E.printstacktrace ();    }}}//set default processor public void init () {Thread.setdefaultuncaughtexceptionhandler (this); } private Crashhandler () {}//Singleton public static Crashhandler instance () {if (Crashhandler = = nul            L) {synchronized (Crashhandler) {Crashhandler = new Crashhandler ();    }} return Crashhandler; }}
Step three: Invoke the Exception capture feature in application

Import android.app.Application;    public class MainApplication extends application {      @Override public      void OnCreate () {          super.oncreate ();          Crashhandler Crashhandler = Crashhandler.getinstance ();          Crashhandler.init (this);      }  }  
Fourth step: Application registration in the manifest file
Application and activity all need to be registered in the manifest file, but when we build the project, the development tools automatically register the application in the project, and we define ourselves to manually replace the default.

<application android:name= ". MainApplication "         android:icon=" @drawable/ic_launcher "         android:label=" @string/app_name "         android: Theme= "@style/apptheme" > ...  </application>  
Talk about here is finished, if you put these code into the, and so your program crashes, will give you automatic record, of course, these are just for you to record to the local, and stored as a file, if you want to upload to the server, also ask the students to do their own, the file upload it! Uploading files to the server I think the students will be all right!

To tell the truth, write an article really sometimes more energy-intensive, if the students do not abandon or feel useful to themselves, but also please readily forwarded to the circle of friends, so that more of their friends benefit, but also let more people to pay attention to me to write the power of the article.

Public Number: smart_android, public number [non-famous programmer] may be the best technology in the old hemisphere to share the public number. Every day, we push some original articles and tutorials about mobile development on a weekly schedule.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android will crash information on how to save the application to a local file and upload the server

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.