android异常捕捉,Android捕获崩溃异常

由于我们写的代码难免会出现一些bug,以及由于测试环境和生产环境差异导致出现的一些异常,在测试过程中没有发现,而在app上线之后会偶然出现的一些bug,以至于app在使用过程中出现ANR,这是个令人蛋疼的现象,app卡死、出现黑屏等,总之当app出现异常时的用户体验不友好,我们开发者需要去捕获这些异常,收集这些异常信息,并且上传到服务器,利于开发人员去解决这些问题,同时,我们还需要给用户一个友好的交互体验。

我也查找了许多捕获崩溃异常的文章来看,但大多都千篇一律,只说了怎么捕获异常,处理异常,并没有对捕获异常后的界面交互做出很好的处理,系统出现ANR时会先卡一段时间(这个时间还有点长)然后弹出一个系统默认的对话框,但是我现在需要的是当出现异常情况时,立马弹出对话框,并且对话框我想自定义界面。

最终实现的效果如图:

最终效果图

首先需要自定义Application,并且在AndroidManifest.xml中进行配置

fb28a5322d8a

AndroidManifest.xml.png

还需要自定义一个应用异常捕获类AppUncaughtExceptionHandler,它必须得实现Thread.UncaughtExceptionHandler接口,另外还需要重写uncaughtException方法,去按我们自己的方式来处理异常

在Application中我们只需要初始化自定义的异常捕获类即可:

@Override public void onCreate() {

super.onCreate();

mInstance = this;

// 初始化文件目录

SdcardConfig.getInstance().initSdcard();

// 捕捉异常

AppUncaughtExceptionHandler crashHandler = AppUncaughtExceptionHandler.getInstance();

crashHandler.init(getApplicationContext());

}

其中文件目录是异常信息保存在sd卡中的目录,还有我们是整个app中全局捕获异常,所以我们自定义的捕获类是单例。

/**

* 初始化捕获类

*

* @param context

*/

public void init(Context context) {

applicationContext = context.getApplicationContext();

crashing = false;

//获取系统默认的UncaughtException处理器

mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();

//设置该CrashHandler为程序的默认处理器

Thread.setDefaultUncaughtExceptionHandler(this);

}

完成以上过程后,接着需要重写uncaughtException方法:

@Override

public void uncaughtException(Thread thread, Throwable ex) {

if (crashing) {

return;

}

crashing = true;

// 打印异常信息

ex.printStackTrace();

// 我们没有处理异常 并且默认异常处理不为空 则交给系统处理

if (!handlelException(ex) && mDefaultHandler != null) {

// 系统处理

mDefaultHandler.uncaughtException(thread, ex);

}

byebye();

}

private void byebye() {

android.os.Process.killProcess(android.os.Process.myPid());

System.exit(0);

}

既然是我们自己处理异常,所以会先执行handlelException(ex)方法:

private boolean handlelException(Throwable ex) {

if (ex == null) {

return false;

}

try {

// 异常信息

String crashReport = getCrashReport(ex);

// TODO: 上传日志到服务器

// 保存到sd卡

saveExceptionToSdcard(crashReport);

// 提示对话框

showPatchDialog();

} catch (Exception e) {

return false;

}

return true;

}

获取的异常信息包括系统信息,app版本信息,以及手机制造商信息等:

/**

* 获取异常信息

* @param ex

* @return

*/

private String getCrashReport(Throwable ex) {

StringBuffer exceptionStr = new StringBuffer();

PackageInfo pinfo = CrashApplication.getInstance().getLocalPackageInfo();

if (pinfo != null) {

if (ex != null) {

//app版本信息

exceptionStr.append("App Version:" + pinfo.versionName);

exceptionStr.append("_" + pinfo.versionCode + "\n");

//手机系统信息

exceptionStr.append("OS Version:" + Build.VERSION.RELEASE);

exceptionStr.append("_");

exceptionStr.append(Build.VERSION.SDK_INT + "\n");

//手机制造商

exceptionStr.append("Vendor: " + Build.MANUFACTURER+ "\n");

//手机型号

exceptionStr.append("Model: " + Build.MODEL+ "\n");

String errorStr = ex.getLocalizedMessage();

if (TextUtils.isEmpty(errorStr)) {

errorStr = ex.getMessage();

}

if (TextUtils.isEmpty(errorStr)) {

errorStr = ex.toString();

}

exceptionStr.append("Exception: " + errorStr + "\n");

StackTraceElement[] elements = ex.getStackTrace();

if (elements != null) {

for (int i = 0; i < elements.length; i++) {

exceptionStr.append(elements[i].toString() + "\n");

}

}

} else {

exceptionStr.append("no exception. Throwable is null\n");

}

return exceptionStr.toString();

} else {

return "";

}

}

将异常信息保存到sd卡这个我觉得可选吧,但是上传到服务端还是很有必要的:

/**

* 保存错误报告到sd卡

* @param errorReason

*/

private void saveExceptionToSdcard(String errorReason) {

try {

Log.e("CrashDemo", "AppUncaughtExceptionHandler执行了一次");

String time = mFormatter.format(new Date());

String fileName = "Crash-" + time + ".log";

if (SdcardConfig.getInstance().hasSDCard()) {

String path = SdcardConfig.LOG_FOLDER;

File dir = new File(path);

if (!dir.exists()) {

dir.mkdirs();

}

FileOutputStream fos = new FileOutputStream(path + fileName);

fos.write(errorReason.getBytes());

fos.close();

}

} catch (Exception e) {

Log.e("CrashDemo", "an error occured while writing file..." + e.getMessage());

}

}

保存在sd卡中的异常文件格式:

fb28a5322d8a

异常信息

至于上传到服务器,就比较灵活了,可以将整个文件上传,或者上传异常信息的字符串,可以和后端开发人员配合。

因为捕获异常后我要马上关闭掉app即上面的byebye方法,是将app整个进程杀死,如果接着要显示提示对话框,则需要在新的任务栈中打开activity:

public static Intent newIntent(Context context, String title, String ultimateMessage) {

Intent intent = new Intent();

intent.setClass(context, PatchDialogActivity.class);

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

intent.putExtra(EXTRA_TITLE, title);

intent.putExtra(EXTRA_ULTIMATE_MESSAGE, ultimateMessage);

return intent;

}

对话框中给出了重启操作的选项,重启过程的实现:

private void restart() {

Intent intent = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

startActivity(intent);

super.onDestroy();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值