当前位置:网站首页>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))
}
边栏推荐
- Global and Chinese market of postal automation systems 2022-2028: Research Report on technology, participants, trends, market size and share
- Find books ()
- Optical cat super account password and broadband account password acquisition
- Use of form text box (I) select text
- Zzuli:1057 prime number determination
- Zzuli:1053 sine function
- Global and Chinese markets for flexible chips 2022-2028: Research Report on technology, participants, trends, market size and share
- Time conversion ()
- Yolov5 series (I) -- network visualization tool netron
- [ue4] Niagara's indirect draw
猜你喜欢

The picture quality has been improved! LR enhancement details_ Lightroom turns on AI photo detail enhancement: picture clarity increases by 30%
![[ue4] cascading shadow CSM](/img/83/f4dfda3bd5ba0172676c450ba7693b.jpg)
[ue4] cascading shadow CSM

C language to realize mine sweeping

Zzuli:1053 sine function

ASTC texture compression (adaptive scalable texture compression)

Amazon, express, lazada, shopee, eBay, wish, Wal Mart, Alibaba international, meikeduo and other cross-border e-commerce platforms evaluate how Ziyang account can seize traffic by using products in th

Introduction to opengl4.0 tutorial computing shaders

Dllexport et dllimport
![[engine development] rendering architecture and advanced graphics programming](/img/a4/3526a4e0f68e49c1aa5ce23b578781.jpg)
[engine development] rendering architecture and advanced graphics programming

Remote server background hangs nohup
随机推荐
406. Reconstruct the queue according to height
2021-10-16 initial programming
Global and Chinese markets for transparent OLED displays 2022-2028: Research Report on technology, participants, trends, market size and share
dllexport和dllimport
Zero copy underlying analysis
CentOS7部署哨兵Redis(带架构图,清晰易懂)
Rasterization: a practical implementation (2)
[engine development] rendering architecture and advanced graphics programming
QT program font becomes larger on computers with different resolutions, overflowing controls
Dllexport et dllimport
Zzuli:1059 highest score
Several sentences extracted from the book "leather bag"
[opengl] pre bake using computational shaders
牛客 BM83 字符串变形(大小写转换,字符串反转,字符串替换)
【注意力机制】【首篇ViT】DETR,End-to-End Object Detection with Transformers网络的主要组成是CNN和Transformer
什么是one-hot encoding?Pytorch中,将label变成one hot编码的两种方式
7-3 count the number of words in a line of text
Vs+qt application development, set software icon icon
How to color ordinary landscape photos, PS tutorial
Yolov5 series (I) -- network visualization tool netron