Android UncaughtExceptionHandler捕获崩溃异常

在编写APK程序时,通常会有导致程序崩溃的异常,在通常情况下这些异常不能被捕获到,利用Thread.UncaughtExceptionHandler就可以捕获到这些异常。


在Application类中进行处理:

public class App extends Application implements UncaughtExceptionHandler{

	private static final boolean DEBUG = true;
	private static String TAG = "App";
	private static App app;
	private UncaughtExceptionHandler originalHandler;
	
	@Override
	public void uncaughtException(Thread thread, Throwable ex) {
	    try{
	        File fold = this.getExternalRoot();
	        SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH.mm.ss.SSS" );
	        String fileName = "crash " + sdf.format( new Date() );
	        File file = new File( fold, fileName );
	        FileOutputStream fos = new FileOutputStream( file );
	        PrintWriter ps = new PrintWriter( fos );
	        this.logcat( ps );
	        ex.printStackTrace( ps );
	        ps.close();
	      }catch( Exception e ){
	        Log.e( App.class.getName(), "Can not save exception.", e );
	      }finally{
	        if( this.originalHandler != null ){
	          this.originalHandler.uncaughtException( thread, ex );
	        }else{
	          Log.e( this.getPackageName(), "Thread " + thread.getName() + " exit with uncaught exception.", ex );
	          System.exit( 1 );
	        }
	      }

	}

	  public App(){
		    app = this;
		    if( ! DEBUG ){
		      originalHandler = Thread.getDefaultUncaughtExceptionHandler();
		      Thread.setDefaultUncaughtExceptionHandler( this );
		    }
		  }

	  public File getExternalRoot(){
		    File retVal = Environment.getExternalStorageDirectory();
		    if( retVal != null ){
		      retVal = new File( retVal, this.getPackageName() );
		      if( ! retVal.exists() && ! retVal.mkdir() ){
		        retVal = null;
		      }
		    }
		    return retVal;
		  }
	  
	@Override
	public void onCreate() {
		super.onCreate();
		
		if(!DEBUG){
		      try{
		          File fold = this.getExternalRoot();
		          SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd_HH.mm.ss.SSS" );
		          String fileName = "brains" + sdf.format( new Date() ) + ".log";
		          File file = new File( fold, fileName );
		          FileOutputStream fos = new FileOutputStream( file );
		          PrintStream ps = new PrintStream( fos );
		      
		          com.hgy.brains.log.Log.setLevel( com.hgy.brains.log.Log.VERBOSE );
		          com.hgy.brains.log.Log.setLogOutput( ps );
		        }catch( Exception ex ){
		          android.util.Log.e( TAG, "Can not initialize log output", ex );
		        }
		}
	}
	  private void logcat( PrintWriter ps ){
		    try{
		      ArrayList< String > commandLine = new ArrayList< String >();
		      commandLine.add( "logcat" );
		      commandLine.add( "-d" );
		      commandLine.add( "-v" );
		      commandLine.add( "long" );
		      commandLine.add( "*:V" );
		      
		      Process process = Runtime.getRuntime().exec( commandLine.toArray( new String[ commandLine.size() ] ) );
		      BufferedReader bufferedReader = new BufferedReader( new InputStreamReader( process.getInputStream() ) );
		      
		      String line;
		      String lineSeparator = System.getProperty( "line.separator" );
		      while( ( line = bufferedReader.readLine() ) != null ){
		        ps.append( line );
		        ps.append( lineSeparator );
		      }
		      bufferedReader.close();
		    }catch( IOException e ){
		        Log.e( TAG, "Dump log failed", e);
		    }
		  }  
	  
}
生成对应的crash文件,里面包含了异常的详细信息。

你可能感兴趣的:(Android UncaughtExceptionHandler捕获崩溃异常)