简体   繁体   English

如何防止Android应用程序因后台线程中的异常而崩溃?

[英]How to prevent android app from crashing due to exception in background thread?

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 ). 我有一个应用程序,它正在使用一些第三方库(广告网络提供商SDK - 特别是AdMob SDK,基于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. 我想忽略由此库引起的所有异常,无论其原因如何:在最坏的情况下,应用程序用户将看不到广告 - 它比应用程序崩溃要好得多。

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. 我看到了很多问题,但是其中一些问题太具体了(并没有涵盖我的情况),其他问题则指的是当开发人员控制线程创建并且能够使用try / catch包装整个线程时。 If I still missed the relevant question, covering this case, I will appreciate the link 如果我仍然错过相关问题,涵盖此案例,我将非常感谢这个链接

All you need to do is Extend all the activities with BaseActivity. 您需要做的就是使用BaseActivity扩展所有活动。 The app never crashes at any point 该应用程序永远不会崩溃

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());
            }
        });
    }
}

As mentioned above, Thread.setDefaultUncaughtExceptionHandler is the proper way to handle this. 如上所述,Thread.setDefaultUncaughtExceptionHandler是处理此问题的正确方法。 Create the class: 创建类:

 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());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM