Java 如何防止android应用程序因后台线程异常而崩溃?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22978245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to prevent android app from crashing due to exception in background thread?
提问by Denis Itskovich
It's a general question, which raised from specific scenario, but I'd like to get a general answer how to deal with the following situation:
这是一个普遍的问题,是从特定场景提出的,但我想得到一个如何处理以下情况的一般答案:
Background:
背景:
I have an app, which is using some 3rd party library (ad network provider SDK - specifically - AdMob
SDK, based on Google Play Services
). Functionality of this library is not critical for the application. The library creates one or more background worker threads. Sometimes (very rare case) there is an unhandled exception in one of these background threads, causing to crashing the application. I'd like to ignore all exceptions, caused by this library, regardless of their cause: in worst case the app user will not see an ad - it's much better than app crash.
我有一个应用程序,它使用了一些 3rd 方库(广告网络提供商 SDK - 特别是 - AdMob
SDK,基于Google Play Services
)。该库的功能对应用程序来说并不重要。该库创建一个或多个后台工作线程。有时(非常罕见的情况)在这些后台线程之一中存在未处理的异常,导致应用程序崩溃。我想忽略由该库引起的所有异常,无论其原因如何:在最坏的情况下,应用程序用户将看不到广告 - 这比应用程序崩溃要好得多。
Since the library itself creates the background threads - I cannot just wrap them by try/catch.
由于库本身创建了后台线程 - 我不能只是通过 try/catch 来包装它们。
Question
题
Is there any way to catch all non-handled background (non-main) thread exceptions and just to kill the thread in such case, and to prevent app crash?
有什么方法可以捕获所有未处理的后台(非主)线程异常并在这种情况下杀死线程并防止应用程序崩溃?
Related questions
相关问题
I saw a lot of several questions, but some of them are too specific (and not covering my case), others refer to situation when the developer has a control on thread creation and is able to wrap the whole thread with try/catch. If I still missed the relevant question, covering this case, I will appreciate the link
我看到了很多问题,但其中一些太具体了(并没有涵盖我的情况),其他人指的是开发人员可以控制线程创建并且能够用 try/catch 包装整个线程的情况。如果我仍然错过了相关问题,涵盖了这个案例,我会很感激这个链接
采纳答案by Rohit
All you need to do is Extend all the activities with BaseActivity. The app never crashes at any point
您需要做的就是使用 BaseActivity 扩展所有活动。该应用程序在任何时候都不会崩溃
Code sniplet for BaseActivity :
BaseActivity 的代码片段:
public class BaseActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
public void uncaughtException(Thread paramThread, Throwable paramThrowable) {
Log.e("Error"+Thread.currentThread().getStackTrace()[2],paramThrowable.getLocalizedMessage());
}
});
}
}
回答by Janin
As mentioned above, Thread.setDefaultUncaughtExceptionHandler is the proper way to handle this. Create the class:
如上所述,Thread.setDefaultUncaughtExceptionHandler 是处理此问题的正确方法。创建类:
private class MyThreadUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
Log.e(TAG, "Received exception '" + ex.getMessage() + "' from thread " + thread.getName(), ex);
}
}
Then call setDefaultUncaughtExceptionHandler from your main thread:
然后从主线程调用 setDefaultUncaughtExceptionHandler :
Thread t = Thread.currentThread();
t.setDefaultUncaughtExceptionHandler(new MyThreadUncaughtExceptionHandler());