Android--use Uncaughtexceptionhandler to catch global exceptions

Source: Internet
Author: User

After integrating the Statistics SDK (Friend League statistics, Baidu statistics, etc.), there is a very useful test function: Error analysis! This feature is able to feed back crashes (runtimeexception) problems encountered in the running of the program to the server, helping developers to improve their products and adapt their machines.


However, if the SDK is not integrated in the company's Android development, how should this function be implemented? Let's take a look at how to use Uncaughtexceptionhandler to catch exceptions.


The first implementation creates a class that implements the Uncaughtexceptionhandler interface. The code is as follows:

public class Crashhandler implements Uncaughtexceptionhandler {@Overridepublic void uncaughtexception (thread thread, Throwable ex) {//Handle exception LOG.E ("Crash", Thread.getname () +ex.tostring ());//Send to Server//dialog reminder}}

There is a place to process the code and then you need to trigger it:

Add in the OnCreate of the program's entry activity:

Thread.setdefaultuncaughtexceptionhandler (New Crashhandler ());

is not very easy ah!


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.