What happens to the main function when Android can't see it? The program is abnormal, can not prompt "XXX software stop execution"?

Source: Internet
Author: User

Today, I met this problem and shared the solution.

Android does not have a main function, and naturally there is no solution to add exception handling in main to achieve global exception capture. Does the Android program have a global exception replenishment solution?

The answer is:

That is, you have to inherit the androidproject inside the application, such as:

public class Readerapplication extends application implements thread.uncaughtexceptionhandler{...}

and implement the thread exception replenishment interface: Thread.uncaughtexceptionhandler

This way your program, which only has unhandled exceptions, will be captured in the following uncaughtexception function. My practice is to restart the application.

@Overridepublic void uncaughtexception (thread thread, Throwable ex) {//TODO auto-generated method stub//system.exit (0) ; Intent Intent = Getbasecontext (). Getpackagemanager ().              getlaunchintentforpackage (Getbasecontext (). Getpackagename ());                      Pendingintent restartintent = pendingintent.getactivity (                    getapplicationcontext (), 0, intent,                    Intent.flag_ Activity_new_task);                                                         Exit program                                                  Alarmmanager mgr = (alarmmanager) getsystemservice (context.alarm_service);            Mgr.set (ALARMMANAGER.RTC, System.currenttimemillis () +                    restartintent);//1 seconds after restart application                   system.exit (0);        }


But you have to say that you have to add an exception callback interface to the application OnCreate function:

Thread.setdefaultuncaughtexceptionhandler (this);

Finally, you have to change this sentence into your own application in the Androidmanifest.xml:

<application
Android:name= "Com.founder.reader.ReaderApplication"

Well, with the above global exception handling, there is no use of the so-called main function of the ability to achieve. At the same time, the program will no longer prompt "XXX software stop Execution".

Finally: The program restarts with the system clock to restart, or the program exits, who will run the restart task:

Alarmmanager mgr = (alarmmanager) getsystemservice (Context.alarm_service); Mgr.set (ALARMMANAGER.RTC, System.currenttimemillis () + restartintent); Restart the app after 1 seconds

Finally, do not forget to give their own small site advertising: Program Ape Software: www.uhdesk.com

What happens to the main function when Android can't see it? The program is abnormal, can not prompt "XXX software stop execution"?

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.