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"?