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文件,里面包含了异常的详细信息。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值