1.自定义一个类(MaApp)继承Application
2.在清单文件中的Application选项菜单对应的name属性中添加MyApp
3.重写application中的onCreate方法
4.自定义一个类(MyCrashHandler) 实现UncaughtExceptionHandler实现其中的uncaughtException方法
5.将MyCrashHandler 采用单例设计模式 加入同步代码块
6.在MyApp类中的主线程把这个异常捕获设置进去
1 Thread.currentThread().setUncaughtExceptionHandler(MyCrashHandler.getInstance(getApplicationContext()));
7.异常的处理在MyCrashHandler中的uncaughtException中处理
上代码
MyApp类
1 package cn.itcast.crash; 2 3 import java.lang.Thread.UncaughtExceptionHandler; 4 5 import android.app.Application; 6 7 public class MyApp extends Application { 8 9 10 /** 11 * 应用程序 的进程在第一次被创建的时候 执行的方法 12 */ 13 @Override 14 public void onCreate() { 15 16 Thread.currentThread().setUncaughtExceptionHandler(MyCrashHandler.getInstance(getApplicationContext())); 17 18 19 20 super.onCreate(); 21 } 22 23 24 25 }
MyCrashHandler类
1 package cn.itcast.crash; 2 3 import java.io.File; 4 import java.io.FileOutputStream; 5 import java.io.PrintWriter; 6 import java.io.StringWriter; 7 import java.lang.Thread.UncaughtExceptionHandler; 8 import java.lang.reflect.Field; 9 10 import android.content.Context; 11 import android.content.pm.PackageInfo; 12 import android.os.Build; 13 import android.os.Environment; 14 15 public class MyCrashHandler implements UncaughtExceptionHandler { 16 private MyCrashHandler() { 17 }; 18 private static UncaughtExceptionHandler defaultHandler; 19 //定义一个系统的默认的异常处理的handler 20 private static MyCrashHandler myCrashHandler; 21 private static Context mContext; 22 23 public synchronized static MyCrashHandler getInstance(Context context) { 24 if (myCrashHandler == null) { 25 myCrashHandler = new MyCrashHandler(); 26 mContext = context; 27 defaultHandler = Thread.currentThread().getDefaultUncaughtExceptionHandler(); 28 } 29 return myCrashHandler; 30 } 31 32 /** 33 * 当某一个异常 没有代码显示的捕获的时候, 系统会调用 默认的异常处理的代码 处理这个异常 34 */ 35 @Override 36 public void uncaughtException(Thread thread, Throwable ex) { 37 System.out.println("出现了异常,但是被哥捕获了"); 38 StringWriter wr = new StringWriter(); 39 PrintWriter pw = new PrintWriter(wr); 40 ex.printStackTrace(pw); 41 StringBuilder sb = new StringBuilder(); 42 43 try { 44 PackageInfo packinfo = mContext.getPackageManager().getPackageInfo( 45 mContext.getPackageName(), 0); 46 String version = packinfo.versionName; 47 sb.append("错误信息: "); 48 sb.append("版本号:" + version + " "); 49 50 String errorlog = wr.toString(); 51 sb.append(errorlog); 52 sb.append(" "); 53 54 // 获取当前手机操作系统的信息. 55 Field[] fields = Build.class.getDeclaredFields(); 56 for (Field field : fields) { 57 field.setAccessible(true);// 暴力反射,可以获取私有成员变量的信息 58 String name = field.getName(); 59 String value = field.get(null).toString(); 60 sb.append(name+"="+value+" "); 61 } 62 String time ="time:"+ System.currentTimeMillis(); 63 sb.append(time); 64 String log = sb.toString(); 65 File file = new File(Environment.getExternalStorageDirectory(),"error.log"); 66 FileOutputStream fos = new FileOutputStream(file); 67 fos.write(log.getBytes()); 68 fos.flush(); 69 fos.close(); 70 71 android.os.Process.killProcess(android.os.Process.myPid()); 72 //调用系统的默认的异常处理方法 处理这个异常 73 defaultHandler.uncaughtException(thread, ex); 74 75 } catch (Exception e) { 76 e.printStackTrace(); 77 } 78 79 } 80 81 }