Android UncaughtExceptionHandler完成应用程序 [英] Android UncaughtExceptionHandler to finish app
问题描述
我想在记录未处理的异常后关闭应用程序.在这里搜索后,我做了以下事情:
I want to close the app after logging an unhandled exception. After searching here i made the following:
public class MyApplication extends Application {
//uncaught exceptions
private Thread.UncaughtExceptionHandler defaultUEH;
// handler listener
private Thread.UncaughtExceptionHandler _unCaughtExceptionHandler = new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
//logging code
//..........
//call the default exception handler
defaultUEH.uncaughtException(thread, ex);
}
};
public MyApplication() {
defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(_unCaughtExceptionHandler);
}
}
在呼叫defaultUEH.uncaughtException(thread, ex);
之后,我尝试呼叫System.exit()
以及android.os.Process.killProcess(android.os.Process.myPid());
(甚至我发现有些帖子被告知要同时使用两者).问题是即时通讯出现黑屏,我不得不用电话任务管理器强制退出应用程序.我在做什么错了?
After calling defaultUEH.uncaughtException(thread, ex);
i tried to call System.exit()
and also android.os.Process.killProcess(android.os.Process.myPid());
(even i found some posts where is told to use both). The problem is that im getting a black screen and i have to force the app exit with the phone task manager. What am i doing wrong?
致谢
推荐答案
最后,我解决了此问题,使该类实现了Thread.UncaughtExceptionHandler
接口:
At last i resolved this making a class implementing the Thread.UncaughtExceptionHandler
interface:
public class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
private BaseActivity activity;
private Thread.UncaughtExceptionHandler defaultUEH;
public MyUncaughtExceptionHandler(BaseActivity activity) {
this.activity = activity;
this.defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
}
public void setActivity(BaseActivity activity) {
this.activity = activity;
}
@Override
public void uncaughtException(Thread thread, Throwable ex) {
//LOGGING CODE
//........
defaultUEH.uncaughtException(thread, ex);
}
}
在BaseActivity
中,我添加了以下代码:
In the BaseActivity
i added the following code:
//exception handling
private static MyUncaughtExceptionHandler _unCaughtExceptionHandler;
@Override
protected void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
if(_unCaughtExceptionHandler == null)
_unCaughtExceptionHandler = new MyUncaughtExceptionHandler(this);
else
_unCaughtExceptionHandler.setActivity(this);
if(Thread.getDefaultUncaughtExceptionHandler() != _unCaughtExceptionHandler)
Thread.setDefaultUncaughtExceptionHandler(_unCaughtExceptionHandler);
}
我知道它与问题中的代码相同,但是现在可以正常工作了.当我有更多的空闲时间时,我将对此进行深入研究以找到根本原因并将其发布
I know its the same code i have in the question, but somehow its working now. When i have more free time i will look this deeply to find the root cause and post it
这篇关于Android UncaughtExceptionHandler完成应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!