当前位置:网站首页>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))
}
边栏推荐
猜你喜欢
Vs+qt multithreading implementation -- run and movetothread
5.4-5.5
Yolov5系列(一)——网络可视化工具netron
Dllexport et dllimport
Introduction to opengl4.0 tutorial computing shaders
Centos7 deployment sentry redis (with architecture diagram, clear and easy to understand)
Bucket sorting in C language
dllexport和dllimport
5.2-5.3
复合类型(自定义类型)
随机推荐
Vs+qt multithreading implementation -- run and movetothread
Zhejiang University Edition "C language programming (4th Edition)" topic set reference ideas set
[ue4] material and shader permutation
Global and Chinese market of optical fiber connectors 2022-2028: Research Report on technology, participants, trends, market size and share
Zzuli:1045 numerical statistics
Zzuli:1057 prime number determination
Qt—绘制其他东西
High quality workplace human beings must use software to recommend, and you certainly don't know the last one
Global and Chinese market of transfer case 2022-2028: Research Report on technology, participants, trends, market size and share
C # realizes the login interface, and the password asterisk is displayed (hide the input password)
Zzuli:1055 rabbit reproduction
[graphics] adaptive shadow map
The latest M1 dedicated Au update Adobe audit CC 2021 Chinese direct installation version has solved the problems of M1 installation without flash back!
TPS61170QDRVRQ1
Zzuli:1054 monkeys eat peaches
零拷贝底层剖析
Besides lying flat, what else can a 27 year old do in life?
Global and Chinese markets for transparent OLED displays 2022-2028: Research Report on technology, participants, trends, market size and share
Implement Gobang with C language
Joomla! CMS 3.0~3.4.6 RCE