当前位置:网站首页>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))
}
边栏推荐
- Center and drag linked global and Chinese markets 2022-2028: Research Report on technology, participants, trends, market size and share
- mmdetection 学习率与batch_size关系
- Yolov5进阶之八 高低版本格式转换问题
- 什么是one-hot encoding?Pytorch中,将label变成one hot编码的两种方式
- Global and Chinese markets for indoor HDTV antennas 2022-2028: Research Report on technology, participants, trends, market size and share
- 牛客 BM83 字符串变形(大小写转换,字符串反转,字符串替换)
- 5.4-5.5
- C language STR function
- Zzuli:1049 square sum and cubic sum
- [opengl] bone animation blending effect
猜你喜欢

Bucket sorting in C language

Zero copy underlying analysis
![[wechat applet] wxss template style](/img/28/f9d12bf761e25f9564d92697cf049d.png)
[wechat applet] wxss template style

B2020 分糖果

Solve the problem that PR cannot be installed on win10 system. Pr2021 version -premiere Pro 2021 official Chinese version installation tutorial

【微信小程序】WXSS 模板样式

C string format (decimal point retention / decimal conversion, etc.)

QT program font becomes larger on computers with different resolutions, overflowing controls

【Transform】【NLP】首次提出Transformer,Google Brain团队2017年论文《Attention is all you need》

Devaxpress: range selection control rangecontrol uses
随机推荐
Zzuli:1043 max
Write a 2-minute countdown.
Zzuli: cumulative sum of 1050 factorials
Tencent internship interview sorting
C language DUP function
【微信小程序】WXSS 模板样式
Dllexport and dllimport
复合类型(自定义类型)
Address book sorting
Pytoch deep learning and target detection practice notes
Global and Chinese markets of AC electromechanical relays 2022-2028: Research Report on technology, participants, trends, market size and share
Yolov5进阶之七目标追踪最新环境搭建(二)
High quality workplace human beings must use software to recommend, and you certainly don't know the last one
QT - draw something else
Yolov5 series (I) -- network visualization tool netron
How does vs+qt set the software version copyright, obtain the software version and display the version number?
Zero copy underlying analysis
Web server code parsing - thread pool
Niuke bm83 string deformation (case conversion, string inversion, string replacement)
B2020 points candy