Android UncaughtExceptionHandler捕获崩溃异常
来源:互联网 发布:网络教育概念股 编辑:程序博客网 时间:2024/05/16 07:02
在编写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;@Overridepublic 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; } @Overridepublic 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捕获崩溃异常
- 利用Android UncaughtExceptionHandler捕获崩溃异常
- 利用Android UncaughtExceptionHandler捕获崩溃异常
- 利用Android UncaughtExceptionHandler捕获崩溃异常
- Android UncaughtExceptionHandler捕获线程崩溃异常
- 利用Android的UncaughtExceptionHandler捕获崩溃异常
- android技术学习--Thread.UncaughtExceptionHandler捕获异常崩溃
- Android使用UncaughtExceptionHandler捕获异常
- Android使用UncaughtExceptionHandler捕获异常
- android应用开发-------------应用崩溃全局异常捕获处理(UncaughtExceptionHandler)
- Android捕获崩溃异常
- Android捕获崩溃异常
- Android捕获崩溃异常
- Android使用UncaughtExceptionHandler捕获全局异常
- Android使用UncaughtExceptionHandler捕获全局异常
- Android使用UncaughtExceptionHandler捕获全局异常
- Android使用UncaughtExceptionHandler捕获全局异常
- Android使用UncaughtExceptionHandler捕获全局异常
- android java获取当前时间的总结
- 如何使用Proxy模式及Java内建的动态代理机制
- Hadoop LZO 安装教程
- JVM优化
- 在Linux下使用goagent遇到的问题和解决办法
- Android UncaughtExceptionHandler捕获崩溃异常
- php正则匹配汉字!
- PDF屏蔽打印,隐藏工具栏和菜单栏
- 崩溃
- Secure Network (ssh端口映射)-----图详细可以使用
- normal socket ioctl to net interface ioctl
- GEF 进阶,第六部分: Feedback
- how to mount between Linux servers
- I NEED A OFFER! hdu1203 01背包,概率