平时写代码,我们可能会抛出各种异常,这些异常有些是我们测试过程中发现进行解决的,但是也有一些异常是我们未知的,不论是代码的逻辑问题还是Android本身底层的一些bug,我们都需要及时了解并进行解决。当用户在使用app出现崩溃现象时我们需要知道是什么原因,并将原因记录下来上到服务器,这样以后我们就可以知道具体是什么原因了
##CrashHandler类
用于记录crash原因保存到sd卡中。
public class CrashHandler implements Thread.UncaughtExceptionHandler {
private static final String TAG = "CrashHandler";
private static final boolean DEBUG = true;
private static final String PATH = Environment.getExternalStorageDirectory().getPath() +File.separator+ "crash";
private static final String FILE_NAME = "crash";
private static final String FILE_NAME_SUFEIX = ".trace";
private static Thread.UncaughtExceptionHandler mDefaultCrashHandler;
private static CrashHandler mCrashHandler = new CrashHandler();
private Context mContext;
private CrashHandler() {
}
public static CrashHandler getInstance() {
return mCrashHandler;
}
public void init(Context context) {
mDefaultCrashHandler = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(this);
mContext = context.getApplicationContext();
}
@Override
public void uncaughtException(Thread thread, Throwable ex) {
try {
writeToSDcard(ex);
} catch (IOException e) {
e.printStackTrace();
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
ex.printStackTrace();
if (mDefaultCrashHandler != null) {
mDefaultCrashHandler.uncaughtException(thread, ex);
} else {
Process.killProcess(Process.myPid());
}
}
private void writeToSDcard(Throwable ex) throws IOException, PackageManager.NameNotFoundException {
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
return;
}
File filedir = new File(PATH);
if (!filedir.exists()) {
filedir.mkdirs();
}
long currenttime = System.currentTimeMillis();
String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(currenttime));
File exfile = new File(PATH +File.separator+FILE_NAME+time + FILE_NAME_SUFEIX);
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(exfile)));
Log.e("错误日志文件路径",""+exfile.getAbsolutePath());
pw.println(time);
PackageManager pm = mContext.getPackageManager();
PackageInfo pi = pm.getPackageInfo(mContext.getPackageName(), PackageManager.GET_ACTIVITIES);
pw.println("App Version:" + pi.versionName + "_" + pi.versionCode);
pw.println("OS version:" + Build.VERSION.RELEASE + "_" + Build.VERSION.SDK_INT);
pw.println("Vendor:" + Build.MANUFACTURER);
pw.println("Model:" + Build.MODEL);
pw.println("CPU ABI:" + Build.CPU_ABI);
ex.printStackTrace(pw);
pw.close();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
调用
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
![这里写图片描述](https://img-blog.csdn.net/20160716163601192)