当程序崩溃(Crash)的时候,默认是不对异常信息做处理的。如果想要把异常信息保存到本地文件中,或上传的服务器。那么就要借助UncaughtExceptionHandler这个类。
首先,实例化UncaughtExceptionHandler,代码如下:
public class ForceCloseHandler implements Thread.UncaughtExceptionHandler {
public static final String LOG_FILE_NAME = "forceclose.log";
private static ForceCloseHandler inst ;
public static ForceCloseHandler getInstance() {
if(inst==null)inst = new ForceCloseHandler();
return inst;
}
private Context mContext;
private Thread.UncaughtExceptionHandler mExceptionHandler;
private JSONObject jsonObject;
private ForceCloseHandler() {}
public void init(Context context) {
mContext = context;
mExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(this);
}
private void handleUncaughtException(Throwable ex) {
if (ex == null) return;
try {
if(jsonObject ==null)jsonObject = new JSONObject();
saveDeviceInfo();
saveForceCloseInfo2File(ex);
// 把异常信息发送到服务器
ForceCloseFeedBack.getInstance().feedBack(mContext, jsonObject.toString());
} catch (Exception e) {
LogUtil.w(e);
}
}
@Override
public void uncaughtException(Thread thread, Throwable throwable) {
handleUncaughtException(throwable);
if (throwable != null) LogUtil.w(throwable);
mExceptionHandler.uncaughtException(thread, throwable);
}
public void saveDeviceInfo() throws JSONException {
TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
jsonObject.put("platform", "android");
jsonObject.put("model", Build.MODEL);
jsonObject.put("trackid", AppConfig.PARTNER_ID);
jsonObject.put("product", AppConfig.CLIENT_TAG);
jsonObject.put("os_version", Build.VERSION.RELEASE);
jsonObject.put("deviceid", DeviceInfo.getDeviceId());
jsonObject.put("net_type", tm.getNetworkOperator());
jsonObject.put("timestamp", System.currentTimeMillis());
jsonObject.put("app_version", Application.getInstance().getVersionCode());
}
private void saveForceCloseInfo2File(Throwable ex) throws Exception {
StringWriter writer = new StringWriter();
PrintWriter printWriter = new PrintWriter(writer);
ex.printStackTrace(printWriter);
Throwable cause = ex.getCause();
while (cause != null) {
cause.printStackTrace(printWriter);
cause = cause.getCause();
}
printWriter.close();
String result = writer.toString() + "\n";
jsonObject.put("errorDetail", result);
if (AppConfig.LOG_ERR_SAVE) {
File file = FileHelper.getDiskCacheDir(mContext, LOG_FILE_NAME);
FileOutputStream fos = new FileOutputStream(file, true);
fos.write(jsonObject.toString().getBytes());
fos.close();
}
}
}
接下来,看看这个实例化后的类怎么使用,代码如下:
public class Application extends android.app.Application {
private static Application instance;
public Context currentContext;
public static Application getInstance() {
return instance;
}
@Override
public void onCreate() {
super.onCreate();
init();
}
public void init() {
instance = this;
if (Config.LOG_ERR_FEED) {
ForceCloseHandler.getInstance().init(instance);
}
}
@Override
public void onTerminate() {
super.onTerminate();
}
@Override
public void onLowMemory() {
super.onLowMemory();
}
}
非常简单,只要在指定的Application类的onCreate()回调中,把UncaughtExceptionHandler和Application的实例绑定在一起就可以了。关键代码如下:
public void init(Context context) {
mContext = context;
mExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(this);
}
这样,如果程序崩溃,错误日志就会被保存在本地,同时又被上传到了服务器。
分享到:
相关推荐
Android捕获异常处理——UncaughtExceptionHandler
添加UncaughtExceptionHandler这个类 iOS SDK提供的函数是NSSetUncaughtExceptionHandler来进行异常处理。但是无法处理内存访问错误、重复释放等错误,因为这些错误发送的SIGNAL。所以需要处理这些SIGNAL
此demo可以捕获android的异常,可以捕获ANR+UncaughtExceptionHandler类异常,并输出到本地文件,6.0需要添加动态读写权限
下面让我们来看下如何使用UncaughtExceptionHandler来捕获异常。 首先实现创建一个类,实现UncaughtExceptionHandler接口。代码如下: 代码如下:public class CrashHandler implements UncaughtExceptionHandler {
Android捕获全局异常并写日志保存到sd卡,捕获异常消息后输出这个消息,可避免APP强制退出,抛出NullPointException玩玩哈,捕获全局异常是因为有的异常我们捕获不到,捕获时我们采用同步方法,以免单例多线程环境下...
UncaughtExceptionHandler 来捕获异常
整理完善usb通信(usb打印),可判断设备连接状态和开关机状态,设备连接过程中断开连接后,自动连接. UncaughtExceptionHandler接口实现本地捕获异常log信息保存到本地内存,方便查看调试!
当我们的的应用由于运行时异常导致Force Close的时候,通过设置主线程的UncaughtExceptionHandler来捕获异常信息,同时用户可以把堆栈信息通过发送邮件的方式反馈给我们。
实现UncaughtExceptionHandler拦截android异常,捕获全局异常,CrashHandler完整工程源码
安卓基于UncaughtExceptionHandler实现异常的抓取,并实现使用retrofit网络框架的上传,该压缩包为完整案例
Android 全局异常捕获 今天就来说说作为程序猿的我们每天都会遇到的东西bug,出bug不可怕可怕的是没有出bug时的堆栈信息,那么对于bug的信息收集就显得尤为重要了,一般用第三方bugly或者友盟等等都能轻易收集,但是由于...
但是当程序开发完毕,版本稳定,需要上线时,为了避免抛出异常影响用户感受,可以用UncaughtExceptionHandler捕获全局异常,对异常做出处理。比如我们可以获取到抛出异常的时间、手机的硬件信息、错误的堆栈信息,...
android中用UncaughtExceptionHandler抓取闪退日志
android 全局异常处理 继承java.lang中的UncaughtExceptionHandler 的构造方法uncaughtException 来捕获异常
通过UncaughtExceptionHandler捕获到全局异常信息,写到本地然后通过邮件发送开发者信箱
java UncaughtExceptionHandler:里面我用了多线程,自己导入就可以运行。强大的异常处理不解释,吃透就你知道其中的奥妙。不懂的回帖
使用android:process和Process.killProcess和UncaughtExceptionHandler完成Android应用程序不异常退出
通过应用程序委托中调用[[AppExceptionHandler shareAppExceptionHandler] start];来开启异常捕获;
1、实现UncaughtExceptionHandler,在方法uncaughtException中处理没有捕获的异常。 public class GlobalException implements UncaughtExceptionHandler { private final static GlobalException myCrashHandler ...