Android Crash 报告反馈
1,Java中有一个接口,staticinterfaceThread.UncaughtExceptionHandler当Thread因未捕获的异常而突然终止时,调用处理程序的接口。
2,Java中有一个方法,staticvoidsetDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandlereh)设置当线程由于未捕获到异常而突然终止,并且没有为该线程定义其他处理程序时所调用的默认处理程序。
public class DefaultExceptionHandler implements UncaughtExceptionHandler {
private Context act = null;
public DefaultExceptionHandler(Context act) {
this.act = act;
}
@Override
public void uncaughtException(Thread thread, Throwable ex) {
// 收集异常信息 并且发送到服务器
sendCrashReport(ex);
// 等待半秒
try {
Thread.sleep(500);
} catch (InterruptedException e) {
//
}
// 处理异常
handleException();
}
private void sendCrashReport(Throwable ex) {
StringBuffer exceptionStr = new StringBuffer();
exceptionStr.append(ex.getMessage());
StackTraceElement[] elements = ex.getStackTrace();
for (int i = 0; i < elements.length; i++) {
exceptionStr.append(elements[i].toString());
}
//TODO
//发送收集到的Crash信息到服务器
}
private void handleException() {
//TODO
//这里可以对异常进行处理。
//比如提示用户程序崩溃了。
//比如记录重要的信息,尝试恢复现场。
//或者干脆记录重要的信息后,直接杀死程序。
}
}3,在主Activity的onCreate(BundlesavedInstanceState)方法中增加如下代码。
Thread.setDefaultUncaughtExceptionHandler(newDefaultExceptionHandler(
this.getApplicationContext())
);
4,发送到服务器!
相关推荐
zzqLivecn 2020-07-09
xilove0 2020-07-09
lookingFor 2020-06-20
kururunga 2020-05-07
sgafdsg 2020-04-11
xilove0 2020-02-01
csdnuuu 2020-01-03
安辉 2020-01-01
fengyeezju 2019-12-01
pengjin 2019-12-01
ShareUs 2013-07-12
ruizhenggang 2010-11-05
guizhongyun 2011-08-10
nickey 2011-08-06
chenjinlong 2011-08-04
luoj 2011-05-24
ustcrding 2011-04-23