//测试代码
public class CrashTestDemo {
private static long startWorkTimeMillis = 0L;
public static void test(){
Looper.getMainLooper().setMessageLogging(new Printer() {
@Override
public void println(String it) {
if (it.startsWith(">>>>> Dispatching to Handler")) {
startWorkTimeMillis = System.currentTimeMillis();
} else if (it.startsWith("<<<<< Finished to Handler")) {
long duration = System.currentTimeMillis() - startWorkTimeMillis;
if (duration > 100) {
Log.e("Application---主线程执行耗时过长","$duration 毫秒,$it");
}
}
}
});
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
while (true){
try {
Looper.loop();
} catch (Throwable e){
if (e.getMessage()!=null && e.getMessage().startsWith("Unable to start activity")){
android.os.Process.killProcess(android.os.Process.myPid());
break;
}
e.printStackTrace();
Log.e("Application---Looper---",e.getMessage());
}
}
}
});
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
e.printStackTrace();
Log.e("Application-----","uncaughtException---异步线程崩溃,自行上报崩溃信息");
}
});
}
}
```
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。