当前位置:网站首页>App全局异常捕获
App全局异常捕获
2022-07-03 14:53:00 【墨墨0830】
App异常全局捕获,防止应用crash掉(不防ANR)。
/** * 异常捕获 * */
public class NeverCrash {
private CrashHandler mCrashHandler;
private static NeverCrash mInstance;
private NeverCrash(){
}
private static NeverCrash getInstance(){
if(mInstance == null){
synchronized (NeverCrash.class){
if(mInstance == null){
mInstance = new NeverCrash();
}
}
}
return mInstance;
}
public static void init(CrashHandler crashHandler){
getInstance().setCrashHandler(crashHandler);
}
private void setCrashHandler(CrashHandler crashHandler){
mCrashHandler = crashHandler;
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
for (;;) {
try {
Looper.loop();
} catch (Throwable e) {
if (mCrashHandler != null) {
//捕获异常处理
mCrashHandler.uncaughtException(Looper.getMainLooper().getThread(), e);
}
}
}
}
});
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
if(mCrashHandler!=null){
//捕获异常处理
mCrashHandler.uncaughtException(t,e);
}
}
});
}
public interface CrashHandler{
void uncaughtException(Thread t,Throwable e);
}
}
在application中使用:
NeverCrash.init {
t, e ->
Log.d(TAG, Log.getStackTraceString(e))
}
边栏推荐
- Zzuli:1055 rabbit reproduction
- Zzuli:1056 lucky numbers
- 从书本《皮囊》摘录的几个句子
- TPS61170QDRVRQ1
- 2022/02/14
- NOI OPENJUDGE 1.6(09)
- Qt development - scrolling digital selector commonly used in embedded system
- Yolov5进阶之七目标追踪最新环境搭建(二)
- Troubleshooting method of CPU surge
- Pytoch deep learning and target detection practice notes
猜你喜欢
[graphics] hair simulation in tressfx
[engine development] rendering architecture and advanced graphics programming
Série yolov5 (i) - - netron, un outil de visualisation de réseau
My QT learning path -- how qdatetimeedit is empty
C string format (decimal point retention / decimal conversion, etc.)
【Transform】【NLP】首次提出Transformer,Google Brain团队2017年论文《Attention is all you need》
High quality workplace human beings must use software to recommend, and you certainly don't know the last one
Talking about part of data storage in C language
Dllexport and dllimport
There are links in the linked list. Can you walk three steps faster or slower
随机推荐
C language fcntl function
C language memory function
Introduction to opengl4.0 tutorial computing shaders
406. 根据身高重建队列
创业团队如何落地敏捷测试,提升质量效能?丨声网开发者创业讲堂 Vol.03
Global and Chinese markets for infrared solutions (for industrial, civil, national defense and security applications) 2022-2028: Research Report on technology, participants, trends, market size and sh
Container of symfony
Joomla! CMS 3.0~3.4.6 RCE
QT - draw something else
Piwigo 2.7.1 sqli learning
[engine development] in depth GPU and rendering optimization (basic)
7-9 one way in, two ways out (25 points)
Optical cat super account password and broadband account password acquisition
5.4-5.5
什么是embedding(把物体编码为一个低维稠密向量),pytorch中nn.Embedding原理及使用
Zzuli:1048 factorial table
Zzuli:1059 highest score
Qt—绘制其他东西
Yolov5进阶之七目标追踪最新环境搭建(二)
Global and Chinese markets for flexible chips 2022-2028: Research Report on technology, participants, trends, market size and share