Package COM. example. crashcatch; import Java. io. file; import Java. io. ioexception; import Java. lang. thread. uncaughtexceptionhandler; import Java. lang. reflect. field; import Java. text. simpledateformat; import Java. util. date; import Java. util. hashmap; import Java. util. iterator; import Java. util. map; import Java. util. set; import android. content. context; import android. content. intent; import android. OS. build; import Droid. OS. environment; import android. util. log; import android. widget. toast;/*** handle exceptions that are not caught by the system and prevent poor app crash experience * @ author ywx206812 **/public class crashhandler implements thread. uncaughtexceptionhandler {crashapplication; uncaughtexceptionhandler defaultuncaughtexceptionhandler; simpledateformat = new simpledateformat ("yyyy-mm-dd_HH-mm-ss"); Private Static crashhandler CRAs Hhandler = new crashhandler (); Private crashhandler () {super (); // todo auto-generated constructor stub} public static crashhandler getinstance () {return crashhandler ;} public void Init (crashapplication) {This. crashapplication = crashapplication; defaultuncaughtexceptionhandler = thread. getdefaultuncaughtexceptionhandler (); thread. setdefaultuncaughtexceptionhandler (this) ;}@ overridepublic void u Ncaughtexception (thread, throwable ex) {// todo auto-generated method stubif (! Catchhandler (Ex) {defaultuncaughtexceptionhandler. uncaughtexception (thread, ex) ;}/ *** Exception Handling ** @ Param ex * @ return */Public Boolean catchhandler (throwable ex) {boolean result = false; try {log. E ("------------------ exception capture ------------", "Write document"); Result = saveexceptioninfo (Ex); toast. maketext (crashapplication. getcurrentcontext (), "program exception", toast. length_short ). show (); crashapplication. getcurrentcontext (). sendbroa Dcast (new intent (crashactivity. crashaction); log. E ("------------------ exception capture ------------", "Send broadcast");} catch (illegalargumentexception e) {// todo auto-generated catch blocke. printstacktrace ();} catch (illegalaccessexception e) {// todo auto-generated catch blocke. printstacktrace ();} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace ();} return result;}/*** get the basic information of the device and app * @ Re Turn * @ throws illegalaccessexception * @ throws syntax */Public Map <string, string> getbaseinfo () throws illegalargumentexception, illegalaccessexception {Map <string, string> map = new hashmap <string, string> (); long currtime = system. currenttimemillis (); string timestr = simpledateformat. format (new date (currtime); map. put ("current time", timestr); Class bclass = build. class. getclass (); field [] Field S = bclass. getfields (); For (INT I = 0; I <fields. length; I ++) {string key = Fields [I]. getname (); string val = Fields [I]. get (null ). tostring (); map. put (Key, Val) ;}return map;} public Boolean saveexceptioninfo (throwable ex) throws illegalargumentexception, illegalaccessexception, ioexception {Map <string, string> map = getbaseinfo (); map. put ("", Ex. getmessage (); set <string> keys = map. keyset (); iterator <string> ITER Ator = keys. iterator (); stringbuilder sb = new stringbuilder (); While (iterator. hasnext () {string key = iterator. next (); string val = map. get (key); sb. append (Key + ":" + Val + "\ n");} string packagename = crashapplication. getcurrentcontext (). getpackagename (); If (environment. getexternalstoragestate (). equals (environment. media_mounted) {file sddir = environment. getexternalstoragedirectory (); file dir = new file (sddir, packagen Ame + "/" + "crash_log"); If (! Dir. exists () {dir. mkdirs ();} string currlogtime = simpledateformat. format (new date (system. currenttimemillis (); string filename = "crash _" + currlogtime + ". log "; return sdcardutil. writefile (Dir, filename, sb. tostring () ;}return false ;}}
Package COM. example. crashcatch; import android. app. activity; import android. app. alertdialog; import android. content. broadcastreceiver; import android. content. context; import android. content. dialoginterface; import android. content. intent; import android. content. intentfilter; import android. OS. bundle; import android. util. log; public class crashactivity extends activity {public static final string crashaction = "crashcatch"; Public crashreceiver = new crashreceiver (); @ overrideprotected void oncreate (bundle savedinstancestate) {// todo auto-generated method stubsuper. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); (crashapplication) getapplication ()). setcurrentcontext (this); log. E ("----------------- crashreceiver ---------------", "register broadcast"); intentfilter = new intentfilter (); intentfilter. addaction (crashactivity. crashaction); registerreceiver (crashreceiver, intentfilter); log. E ("----------------- crashreceiver ---------------", "registered");} class crashreceiver extends broadcastreceiver {@ overridepublic void onreceive (context, intent) {// todo auto-generated method stublog. E ("----------------- crashreceiver ---------------", intent. getaction (); If (intent. getaction (). equals (crashaction) {New alertdialog. builder (crashactivity. this ). setmessage ("program exception, exit "). setnegativebutton ("OK", new dialoginterface. onclicklistener () {@ overridepublic void onclick (dialoginterface dialog, int which) {// todo auto-generated method stubunregisterreceiver (crashreceiver); android. OS. process. killprocess (Android. OS. process. mypid (); system. exit (0 );}}). create (). show ();}}}}
SD card Detection
Package COM. example. crashcatch; import Java. io. file; import Java. io. filenotfoundexception; import Java. io. fileoutputstream; import Java. io. filewriter; import Java. io. ioexception; import android. util. log; public class sdcardutil {/*** returned byte array for reading the file * @ Param filepath * @ return */public static byte [] readfile (string filepath) {return New byte [1];}/*** Write File * @ Param filepath * @ Param bytes * @ return * @ throws Ioexception */public static Boolean writefile (string filepath, string content) throws ioexception {file F = GetFile (filepath); If (F! = NULL) {f. delete () ;}f = new file (filepath); try {filewriter fw = new filewriter (f); FW. write (content); FW. close (); log. E ("------------------ Write File ------------", "Write completed"); Return true;} catch (filenotfoundexception e) {// todo auto-generated catch blockthrow E ;} catch (ioexception e) {// todo auto-generated catch blockthrow e ;}} /*** Write File ** @ Param dir * @ Param filename * @ Param bytes * @ return * @ throws ioexcep Tion */public static Boolean writefile (File Dir, string filename, string content) throws ioexception {file F = GetFile (Dir, filename); If (F! = NULL) {f. delete ();} f = new file (Dir, filename); Return writefile (F. getabsolutepath (), content);} public static file GetFile (string filepath) {file = new file (filepath); Return GetFile (File );} public static file GetFile (File file) {If (file. exists () & file. isfile () {return file;} return NULL;} public static file GetFile (File Dir, string filename) {file = new file (Dir, filename ); return GetFile (File );}}