如何prevent Android应用程序从后台线程崩溃由于异常? [英] How to prevent android app from crashing due to exception in background thread?

查看:241
本文介绍了如何prevent Android应用程序从后台线程崩溃由于异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个普遍的问题,它从特定的情况下提出的,但我希望得到一个笼统的回答如何应对以下情况:

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:

背景:

我有一个应用程序,它是使用一些第三方库(广告网络提供的SDK - 专 - AdMob的 SDK,基于谷歌播放服务)。这个库的功能并不是应用程序的关键。库创建一个或多个后台工作线程。有时(非常罕见的情况下)没有在这些后台线程的一个未处理的异常,导致崩溃的应用程序。我想忽略所有的异常,造成这个库,不论其原因:在最坏的情况下应用程序的用户将不会看到广告 - 它比应用程序崩溃好得多。

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.

由于库本身创建后台线程 - 我不能只将它们包装用的try / catch。

Since the library itself creates the background threads - I cannot just wrap them by try/catch.

问题

有没有办法赶上所有的非后台处理(无主)线程例外,只是杀死在这种情况下,线程,和prevent应用程序崩溃?

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?

相关问题

我看到了很多的几个问题,但有些过于具体(而不是覆盖我的情况),其他人参考的情况时,开发商对线程创建一个控制,并能包住与尝试,整个线程/抓住。如果我还是错过了相关的问题,涉及这种情况下,我将AP preciate链接

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

推荐答案

所有你需要做的是延长所有活动BaseActivity。该应用程序从来没有崩溃的任何一点

All you need to do is Extend all the activities with BaseActivity. The app never crashes at any point

code sniplet为BaseActivity:

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

这篇关于如何prevent Android应用程序从后台线程崩溃由于异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆