由于保留部分代码以便您以后找到它的最佳方法是将其发布在“网络”中,
private UnexpectedTerminationHelper mUnexpectedTerminationHelper = new UnexpectedTerminationHelper();
private class UnexpectedTerminationHelper {
private Thread mThread;
private Thread.UncaughtExceptionHandler mOldUncaughtExceptionHandler = null;
private Thread.UncaughtExceptionHandler mUncaughtExceptionHandler = new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable ex) { // gets called on the same (main) thread
XXXX.closeCamera(); // TODO: write appropriate code here
if(mOldUncaughtExceptionHandler != null) {
// it displays the "force close" dialog
mOldUncaughtExceptionHandler.uncaughtException(thread, ex);
}
}
};
void init() {
mThread = Thread.currentThread();
mOldUncaughtExceptionHandler = mThread.getUncaughtExceptionHandler();
mThread.setUncaughtExceptionHandler(mUncaughtExceptionHandler);
}
void fini() {
mThread.setUncaughtExceptionHandler(mOldUncaughtExceptionHandler);
mOldUncaughtExceptionHandler = null;
mThread = null;
}
}
并且,在主线程的适当位置:
mUnexpectedTerminationHelper.init();
和
mUnexpectedTerminationHelper.fini();