Android异常处理最佳实践
作者:希尔瓦娜斯女神 时间:2021-06-15 20:17:12
一个好的app 异常处理机制 我认为应该至少包含以下几个功能:
1.能把错误信息上传到服务器 让开发者可以持续改进app
2.错误信息至少应该包含 是否在主进程 是否在主线程 等可以帮助程序员定位的信息
3.最好包含手机硬件及软件信息。
4.主进程引发的异常 最好交由系统自己处理 也就是让用户可以感知到 那种(当然你也可以自己定义一套更有意思的感知系统对话框等,具体可参考各种有意思的404界面)
5.子进程引发的异常最好别让用户感知到。比如push之类的 这种 和用户感知弱关联的这种。最好发生异常就直接kill掉即可。不要再交给系统处理。
下面上代码。
package com.example.administrator.exceptiontest;
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
/**
* Created by Administrator on 2015/12/9.
*/
public class BaseApplication extends Application {
public static Context mContext;
//默认的异常处理
public static Thread.UncaughtExceptionHandler defaultUncaughtExceptionHandler;
@Override
public void onCreate() {
super.onCreate();
mContext = this;
//先取得默认的异常处理handler
defaultUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(new BaseUncaughtExceptionHandler());
}
}
package com.example.administrator.exceptiontest;
import android.app.ActivityManager;
import android.content.Context;
import android.os.Looper;
/**
* Created by Administrator on 2015/12/9.
*/
public class Utils {
/**
* 判断是否在主线程中执行 如果是返回true 不是返回false
*/
public static boolean isInMainThread() {
//注意这个地方 我们无法一定可以取得myLooper的值的 比如说 你的thread 没有绑定消息循环
//那你的mylooper就返回的一定是null了,只有绑定了以后才会返回相应的值
return Looper.myLooper() == Looper.getMainLooper();
}
//判断是否是主进程 如果是 就返回true 否则返回false
public static boolean isMainProcess(Context context)
{
return context.getPackageName().equals(getProcessName(context));
}
//取得进程名
public static String getProcessName(Context context) {
String currentProcessName = "";
int pid = android.os.Process.myPid();
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) {
if (processInfo.pid == pid) {
currentProcessName = processInfo.processName;
break;
}
}
return currentProcessName;
}
}
package com.example.administrator.exceptiontest;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
/**
* Created by Administrator on 2015/12/9.
*/
public class BaseUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
Writer resultWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(resultWriter);
ex.printStackTrace(printWriter);
StringBuffer sb = new StringBuffer();
sb.append("是否在主线程中发生异常" + Utils.isInMainThread() + "\n");
sb.append("是否在主进程中发生异常" + Utils.isMainProcess(BaseApplication.mContext) + "\n");
String errorReport = sb.toString() + resultWriter.toString();
//这个地方最好把收集到的错误信息日志 上传至服务器 便于开发者定位修改问题。
//如果是主进程发生了异常 那还是交给系统自己默认的异常处理来做。让用户感知到,否则用户啥都不知道 体验并不好
//你当然也可以自己定义特殊的错误提示 比如一些有意思的dialog什么的
if (Utils.isMainProcess(BaseApplication.mContext)) {
BaseApplication.defaultUncaughtExceptionHandler.uncaughtException(thread, ex);
} else {
//如果是子进程发生异常 不要给提示 直接殺掉子進程最好 別讓用戶感知到
android.os.Process.killProcess(android.os.Process.myPid());
}
}
}
标签:Android,异常处理
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Maven是什么?Maven的概念+作用+仓库的介绍+常用命令的详解
2022-01-21 01:12:24
![](https://img.aspxhome.com/file/2023/7/106117_0s.png)
Java面试题冲刺第二十三天--分布式
2023-09-24 07:30:43
![](https://img.aspxhome.com/file/2023/8/58588_0s.png)
C#使用RabbitMq队列(Sample,Work,Fanout,Direct等模式的简单使用)
2023-12-06 10:45:34
![](https://img.aspxhome.com/file/2023/2/83302_0s.png)
Spring Boot实现配置文件的自动加载和刷新功能
2023-07-14 02:26:55
![](https://img.aspxhome.com/file/2023/1/82381_0s.png)
c#使用filesystemwatcher监视文件系统的变化
2022-08-06 15:28:43
springmvc接收json串,转换为实体类List方法
2023-07-28 12:40:00
elasticsearch集群发现zendiscovery的Ping机制分析
2021-05-25 05:40:55
![](https://img.aspxhome.com/file/2023/2/60572_0s.jpg)
详解idea从git上拉取maven项目详细步骤
2023-04-23 08:51:56
![](https://img.aspxhome.com/file/2023/5/83115_0s.png)
一篇文章带你搞定JAVA注解
2023-03-15 05:22:23
![](https://img.aspxhome.com/file/2023/2/63012_0s.png)
IDEA插件之彩虹括号Rainbow Brackets使用介绍
2022-03-14 09:09:51
![](https://img.aspxhome.com/file/2023/5/98125_0s.png)
详解SpringBoot禁用Swagger的三种方式
2022-02-28 23:49:08
Unity利用材质自发光实现物体闪烁
2021-07-03 20:42:26
![](https://img.aspxhome.com/file/2023/7/87047_0s.gif)
Base64编码解码原理及C#编程实例
2022-05-07 03:58:53
![](https://img.aspxhome.com/file/2023/8/70438_0s.png)
Java异步处理机制实例详解
2022-05-12 01:39:37
Java运行时数据区概述详解
2023-10-08 07:00:10
![](https://img.aspxhome.com/file/2023/0/69130_0s.png)
C#开发WinForm根据条件改变DataGridView行颜色
2022-05-06 05:38:56
![](https://img.aspxhome.com/file/2023/0/92880_0s.jpg)
Android仿微信联系人字母排序效果
2021-10-01 16:06:25
![](https://img.aspxhome.com/file/2023/1/90121_0s.gif)
MyBatis学习教程(五)-实现关联表查询方法详解
2021-06-23 21:04:04
![](https://img.aspxhome.com/file/2023/9/74979_0s.png)
MyBatis自定义映射关系和关联查询实现方法详解
2021-12-18 21:25:28
![](https://img.aspxhome.com/file/2023/2/60902_0s.png)
C#通过xpath查找xml指定元素的方法
2023-06-22 11:55:29