Reprint: http://blog.csdn.net/way_ping_li/article/details/8487866
Write the debug information of your application to the SD card.
Package Com.sdmc.hotel.util;import Java.io.BufferedReader; Import Java.io.File; Import java.io.FileNotFoundException; Import Java.io.FileOutputStream; Import java.io.IOException; Import Java.io.InputStreamReader; Import Android.content.Context; Import android.os.Environment; Import Android.util.Log; /** Log Log statistics save * will print i,e,w, will not print d * Every time the app is opened, the last log message will be overwritten * @author the means **/ Public classLogcathelper {Private StaticLogcathelper INSTANCE =NULL; Private StaticString Path_logcat; PrivateLogdumper Mlogdumper =NULL; Private intmPId; /** * * Initialize directory * **/ Public voidinit (Context context) {if(Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {//priority saving to SD cardPath_logcat =environment.getexternalstoragedirectory (). GetAbsolutePath ()+ File.separator +"Minigps"; } Else{//if the SD card does not exist, it is saved to the application directoryPath_logcat =Context.getfilesdir (). GetAbsolutePath ()+ File.separator +"Minigps"; } File File=NewFile (PATH_LOGCAT); if(!file.exists ()) {File.mkdirs (); } } Public StaticLogcathelper getinstance (context context) {if(INSTANCE = =NULL) {INSTANCE=NewLogcathelper (context); } returnINSTANCE; } PrivateLogcathelper (Context context) {init (context); MPId=Android.os.Process.myPid (); } Public voidstart () {if(Mlogdumper = =NULL) {Mlogdumper=NewLogdumper (string.valueof (mPId), path_logcat); } log.i ("Path", Path_logcat);///Storage/sdcard0/minigpsMlogdumper.start (); } Public voidStop () {if(Mlogdumper! =NULL) {mlogdumper.stoplogs (); Mlogdumper=NULL; } } Private classLogdumper extends Thread {PrivateProcess Logcatproc; PrivateBufferedReader Mreader =NULL; PrivateBoolean mrunning =true; String Cmds=NULL; PrivateString MPID; PrivateFileOutputStream out=NULL; Publiclogdumper (String pid, string dir) {MPID=pid; Try { out=NewFileOutputStream (NewFile (dir,"gps-"+ mydate.getfilename () +". Log")); } Catch(FileNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } /** * * log level: *:v, *:d, *:w, *:e, *:f, *:s * * Show the current MPID program E and W Level of the log. * * */ //Cmds = "Logcat *:e *:w | grep \ "(" + MPID + ") \" "; //cmds = "Logcat | grep \ "(" + MPID + ") \" ";//Print all log information//Cmds = "Logcat-s";//Print label filter InformationCmds ="logcat *:e *:i | grep \ "("+ MPID +")\"";//will print i,e,w and will not print D } Public voidstoplogs () {mrunning=false; } @Override Public voidrun () {Try{Logcatproc=runtime.getruntime (). exec (CMDS); Mreader=NewBufferedReader (NewInputStreamReader (Logcatproc.getinputstream ()),1024x768); String Line=NULL; while(mrunning && (line = Mreader.readline ())! =NULL) { if(!mrunning) { Break; } if(line.length () = =0) { Continue; } if( out!=NULL&&line.contains (MPID)) { out. Write ((Mydate.getdateen () +" "+ Line +"\ n"). GetBytes ()); } } } Catch(IOException e) {e.printstacktrace (); } finally { if(Logcatproc! =NULL) {Logcatproc.destroy (); Logcatproc=NULL; } if(Mreader! =NULL) { Try{mreader.close (); Mreader=NULL; } Catch(IOException e) {e.printstacktrace (); } } if( out!=NULL) { Try { out. Close (); } Catch(IOException e) {e.printstacktrace (); } out=NULL; } } } } }
System permissions
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_LOGS"
Time Tool Class:
Package Com.sdmc.hotel.util;import Java.text.SimpleDateFormat; Import Java.util.Date; Public classMyDate { Public StaticString GetFileName () {SimpleDateFormat format=NewSimpleDateFormat ("YYYY-MM-DD"); String Date= Format.format (NewDate (System.currenttimemillis ())); returnDate//October 03, 2012 23:41:31 } Public StaticString Getdateen () {SimpleDateFormat format1=NewSimpleDateFormat ("YYYY-MM-DD HH:mm:ss"); String Date1= Format1.format (NewDate (System.currenttimemillis ())); returnDate1;//2012-10-03 23:41:31 } }
Method is called:
Public class MyApplication extends Application { @Override publicvoid onCreate () { Logcathelper.getinstance (this). Start (); }
}
Pspspspspsp
Android will apply debug log information saved on SD card