平时写代码,我们可能会抛出各种异常,这些异常有些是我们测试过程中发现进行解决的,但是也有一些异常是我们未知的,不论是代码的逻辑问题还是Android本身底层的一些bug,我们都需要及时了解并进行解决。当用户在使用app出现崩溃现象时我们需要知道是什么原因,并将原因记录下来上到服务器,这样以后我们就可以知道具体是什么原因了
CrashHandler类--用于记录crash原因保存到sd卡中。
1 public class CrashHandler implements Thread.UncaughtExceptionHandler { 2 private static final String TAG = "CrashHandler"; 3 private static final boolean DEBUG = true; 4 //文件路径 5 private static final String PATH = Environment.getExternalStorageDirectory().getPath() +File.separator+ "crash"; 6 private static final String FILE_NAME = "crash"; 7 private static final String FILE_NAME_SUFEIX = ".trace"; 8 private static Thread.UncaughtExceptionHandler mDefaultCrashHandler; 9 private static CrashHandler mCrashHandler = new CrashHandler(); 10 private Context mContext; 11 12 private CrashHandler() { 13 } 14 15 public static CrashHandler getInstance() { 16 return mCrashHandler; 17 } 18 19 public void init(Context context) { 20 mDefaultCrashHandler = Thread.getDefaultUncaughtExceptionHandler(); 21 Thread.setDefaultUncaughtExceptionHandler(this); 22 mContext = context.getApplicationContext(); 23 } 24 25 @Override 26 public void uncaughtException(Thread thread, Throwable ex) { 27 try { 28 //将文件写入sd卡 29 writeToSDcard(ex); 30 //写入后在这里可以进行上传操作 31 } catch (IOException e) { 32 e.printStackTrace(); 33 } catch (PackageManager.NameNotFoundException e) { 34 e.printStackTrace(); 35 } 36 ex.printStackTrace(); 37 //如果系统提供了默认异常处理就交给系统进行处理,否则自己进行处理。 38 if (mDefaultCrashHandler != null) { 39 mDefaultCrashHandler.uncaughtException(thread, ex); 40 } else { 41 Process.killProcess(Process.myPid()); 42 } 43 } 44 45 //将异常写入文件 46 private void writeToSDcard(Throwable ex) throws IOException, PackageManager.NameNotFoundException { 47 //如果没有SD卡,直接返回 48 if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { 49 return; 50 } 51 File filedir = new File(PATH); 52 if (!filedir.exists()) { 53 filedir.mkdirs(); 54 } 55 long currenttime = System.currentTimeMillis(); 56 String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(currenttime)); 57 58 File exfile = new File(PATH +File.separator+FILE_NAME+time + FILE_NAME_SUFEIX); 59 PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(exfile))); 60 Log.e("错误日志文件路径",""+exfile.getAbsolutePath()); 61 pw.println(time); 62 PackageManager pm = mContext.getPackageManager(); 63 PackageInfo pi = pm.getPackageInfo(mContext.getPackageName(), PackageManager.GET_ACTIVITIES); 64 //当前版本号 65 pw.println("App Version:" + pi.versionName + "_" + pi.versionCode); 66 //当前系统 67 pw.println("OS version:" + Build.VERSION.RELEASE + "_" + Build.VERSION.SDK_INT); 68 //制造商 69 pw.println("Vendor:" + Build.MANUFACTURER); 70 //手机型号 71 pw.println("Model:" + Build.MODEL); 72 //CPU架构 73 pw.println("CPU ABI:" + Build.CPU_ABI); 74 75 ex.printStackTrace(pw); 76 pw.close(); 77 78 } 79 80 }
调用
public class BaseApplication extends Application { @Override public void onCreate() { super.onCreate(); CrashHandler crashHandler=CrashHandler.getInstance(); crashHandler.init(this); } }
测试
mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { throw new RuntimeException("抛出一个异常"); } });
最后记得添加文件权限,并调用BaseApplication
发送给后台有点类似与用户注册
友情链接: