我们知道,不论程序怎么写都很难避免不会crash,。当程序出现crash之后,虽然我们无法使其进行运行,但是如果可以知道crash的具体原因,那么我们就可以修复错误。很多时候,产品发布之后,如果用户出现了crash,这个crash信息是很难获取到 的,非常不利于产品的持续发展。
虽然目前市面上有不少提供异常信息回传的第三方sdk,但是我个人认为了解这方面的原理也非常重要。一方面,如果第三方平台出现问题,我们可能一段时间(几个小时到几天不等)无法查看异常信息;另一方面,在一些特殊的场景下,光知道异常的原因很难定位出问题的位置,我们很可能还需要其他的异常数据结合crash信息才能具体定位。
用CrashHandler来获取应用的crash信息
Android应用不可避免地会发生crash,无论你的程序写得多么完美总是无法完全避免crash的发生,可能是Android系统底层的bug,也可能是由于不充分的机型适配或者糟糕的网络状况。当crash发生的时候,系统会kill掉正在执行的程序,现象就是闪退或者提示用户程序已经停止运行,这样的体验对用户来说是很不友好的。幸运的是,Android提供了处理这类问题的方法,情况下面Thread类的一个方法setDefaultUncaughtExceptionHandler:
public void setUncaughtExceptionHandler(UncaughtExceptionHandler eh) {
checkAccess();
uncaughtExceptionHandler = eh;
}
从字面意思看,这个方法好像是可以设置系统的默认异常处理器,其实用这个方法就可以解决上面提到的crash问题。当crash发生的时候,系统会调用UncauchtExcepitonHandler的uncaughtException方法,在uncaughtException方法中就可以获取到异常信息,可以选择把异常信息存储到SD卡中,然后再合适的时机通过网络把crash信息发送回来。同时,可以可以在crash发生时,弹出对话框告诉用户程序crash了,然后退出。
public class CrashHandler implements Thread.UncaughtExceptionHandler {
private static CrashHandler INSTANCE;
private static Context mContext;
private static PendingIntent restartIntent;
private Thread.UncaughtExceptionHandler mDefaultHandler;
private CrashUploader crashUploader;
private DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
public static final String EXCEPTION_INFO = "EXCEPTION_INFO";
public static final String PACKAGE_INFO = "PACKAGE_INFO";
public static final String DEVICE_INFO = "DEVICE_INFO";
public static final String SYSTEM_INFO = "SYSTEM_INFO";
public static final String SECURE_INFO = "SECURE_INFO";
public static final String MEM_INFO = "MEM_INFO";
private String mExceptionInfo;
private String mMemInfo;
private ConcurrentHashMap<String, String> mPackageInfo = new ConcurrentHashMap<>();
private ConcurrentHashMap<String, String> mDeviceInfo = new ConcurrentHashMap<>();
private ConcurrentHashMap<String, String> mSystemInfo = new ConcurrentHashMap<>();
private ConcurrentHashMap<String, String> mSecureInfo = new ConcurrentHashMap<>();
private ConcurrentHashMap<String, Object> totalInfo = new ConcurrentHashMap<>();
private CrashHandler() {
}
public static CrashHandler getInstance() {
if (INSTANCE == null) {
synchronized (CrashHandler.class) {
if (INSTANCE == null) {
INSTANCE = new CrashHandler();
}
}
}
return INSTANCE;
}
public void init(Context context, CrashUploader crashUploader, PendingIntent pendingIntent) {
mContext = context;
this.crashUploader = crashUploader;
this.restartIntent = pendingIntent;
//保存一份系统默认的CrashHandler
mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
//使用我们自定义的异常处理器替换程序默认的
Thread.setDefaultUncaughtExceptionHandler(this);
}
/**
* @param t 出现未捕获异常的线程
* @param e 未捕获的异常,有了这个ex,我们就可以得到异常信息
*/
@Override
public void uncaughtException(Thread t, Throwable e) {
if (!catchCrashException(e) && mDefaultHandler != null) {
//没有自定义的CrashHandler的时候就调用系统默认的异常处理方式
mDefaultHandler.uncaughtException(t, e);
} else {
//退出应用
killProcess();
}
}
/**
* 自定义错误处理,收集错误信息 发送错误报告等操作均在此完成.
*
* @param ex 未捕获的异常
* @return true:如果处理了该异常信息;否则返回false.
*/
private boolean catchCrashException(Throwable ex) {
if (ex == null) {
return false;
}
new Thread() {
@Override
public void run() {
Looper.prepare();
Toast.makeText(ClientApp.getInstance().getApplicationContext(), "Duang~~崩啦~~崩啦~~~~", Toast.LENGTH_SHORT).show();
Looper.loop();
}
}.start();
collectInfo(ex);
//保存日志文件
saveCrashInfo2File();
//上传崩溃信息
uploadCrashMessage(totalInfo);
return true;
}
/**
* 退出应用
*/
private static void killProcess() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Log.e("application", "error : ", e);
}
// 退出程序
AlarmManager mgr = (AlarmManager) ClientApp.getInstance().getApplicationContext().getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 2000, restartIntent); // 2秒钟后重启应用
Process.killProcess(Process.myPid());
System.exit(1);
}
/**
* 获取异常信息和设备参数信息
*/
private void collectInfo(Throwable ex) {
mExceptionInfo = collectExceptionInfo(ex);
collectPackageInfo();
collectBuildInfos();
collectSystemInfos();
collectSecureInfo();
mMemInfo = collectMemInfo();
totalInfo.put(EXCEPTION_INFO, mExceptionInfo);
totalInfo.put(PACKAGE_INFO, mPackageInfo);
totalInfo.put(DEVICE_INFO, mDeviceInfo);
totalInfo.put(SYSTEM_INFO, mSecureInfo);
totalInfo.put(SECURE_INFO, mSecureInfo);
totalInfo.put(MEM_INFO, MEM_INFO);
}
/**
* 获取捕获异常的信息
*/
private String collectExceptionInfo(Throwable ex) {
Writer mWriter = new StringWriter();
PrintWriter mPrintWriter = new PrintWriter(mWriter);
ex.printStackTrace(mPrintWriter);
ex.printStackTrace();
Throwable mThrowable = ex.getCause();
// 迭代栈队列把所有的异常信息写入writer中
while (mThrowable != null) {
mThrowable.printStackTrace(mPrintWriter);
// 换行 每个个异常栈之间换行
mPrintWriter.append("\r\n");
mThrowable = mThrowable.getCause();
}
// 记得关闭
mPrintWriter.close();
return mWriter.toString();
}
/**
* 获取应用包参数信息
*/
private void collectPackageInfo() {
try {
// 获得包管理器
PackageManager mPackageManager = mContext.getPackageManager();
// 得到该应用的信息,即主Activity
PackageInfo mPackageInfo = mPackageManager.getPackageInfo(mContext.getPackageName(), PackageManager.GET_ACTIVITIES);
if (mPackageInfo != null) {
String versionName = mPackageInfo.versionName == null ? "null" : mPackageInfo.versionName;
String versionCode = mPackageInfo.versionCode + "";
this.mPackageInfo.put("VersionName", versionName);
this.mPackageInfo.put("VersionCode", versionCode);
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
}
/**
* 从系统属性中提取设备硬件和版本信息
*/
private void collectBuildInfos() {
// 反射机制
Field[] mFields = Build.class.getDeclaredFields();
// 迭代Build的字段key-value 此处的信息主要是为了在服务器端手机各种版本手机报错的原因
for (Field field : mFields) {
try {
field.setAccessible(true);
mDeviceInfo.put(field.getName(), field.get("").toString());
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
/**
* 获取系统常规设定属性
*/
private void collectSystemInfos() {
Field[] fields = Settings.System.class.getFields();
for (Field field : fields) {
if (!field.isAnnotationPresent(Deprecated.class)
&& field.getType() == String.class) {
try {
String value = Settings.System.getString(mContext.getContentResolver(), (String) field.get(null));
if (value != null) {
mSystemInfo.put(field.getName(), value);
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
/**
* 获取系统安全设置信息
*/
private void collectSecureInfo() {
Field[] fields = Settings.Secure.class.getFields();
for (Field field : fields) {
if (!field.isAnnotationPresent(Deprecated.class)
&& field.getType() == String.class
&& field.getName().startsWith("WIFI_AP")) {
try {
String value = Settings.Secure.getString(mContext.getContentResolver(), (String) field.get(null));
if (value != null) {
mSecureInfo.put(field.getName(), value);
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
/**
* 获取内存信息
*/
private String collectMemInfo() {
BufferedReader br = null;
StringBuffer sb = new StringBuffer();
ArrayList<String> commandLine = new ArrayList<>();
commandLine.add("cat");
commandLine.add("/proc/meminfo");
commandLine.add(Integer.toString(Process.myPid()));
try {
java.lang.Process process = Runtime.getRuntime()
.exec(commandLine.toArray(new String[commandLine.size()]));
br = new BufferedReader(new InputStreamReader(process.getInputStream()), 1024);
while (true) {
String line = br.readLine();
if (line == null) {
break;
}
sb.append(line);
sb.append("\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sb.toString();
}
/**
* 将崩溃日志信息写入本地文件
*/
private String saveCrashInfo2File() {
StringBuffer mStringBuffer = getInfoStr(mPackageInfo);
mStringBuffer.append(mExceptionInfo);
// 保存文件,设置文件名
String mTime = formatter.format(new Date());
String mFileName = "crash-" + mTime + ".log";
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
try {
File mDirectory = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/hive-crash");
if (!mDirectory.exists()) {
boolean success = mDirectory.mkdirs();
}
FileOutputStream mFileOutputStream = new FileOutputStream(mDirectory + File.separator + mFileName);
mFileOutputStream.write(mStringBuffer.toString().getBytes());
mFileOutputStream.close();
return mFileName;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* 将HashMap遍历转换成StringBuffer
*/
@NonNull
private static StringBuffer getInfoStr(ConcurrentHashMap<String, String> info) {
StringBuffer mStringBuffer = new StringBuffer();
for (Map.Entry<String, String> entry : info.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
mStringBuffer.append(key + "=" + value + "\r\n");
}
return mStringBuffer;
}
/**
* 上传崩溃信息到服务器
*/
private void uploadCrashMessage(ConcurrentHashMap<String, Object> info) {
crashUploader.uploadCrashMessage(info);
}
/**
* 崩溃信息上传接口回调
*/
public interface CrashUploader {
void uploadCrashMessage(ConcurrentHashMap<String, Object> info);
}
}
除了通过接口上传到服务器,我们也可以用更多途径传回来,比如,有些第三方提供机器人功能,我们可以通过机器人发消息详见Android捕捉异常信息推送到钉钉群,当然也有更多的方式,我自己会在项目里加一些重要的异常信息用邮件发送回来。
如何使用上面的CrashHandler呢?很简单,可以选择在Application初始化的时候为线程设置CrashHandler,如下所示:
public class App extends Application{
@Override
public void onCreate(){
super.onCreate();
//在这里为应用设置异常处理,然后程序才能获取未处理的异常
CrashHandler crashHandler=CrashHandler.getInstance();
crashHandler.init(this);
}
}
好了,常用的crash处理就说到这里,如果有什么不明白或者其他想法,欢迎留言