全球范围内捕捉一切可能的Andr​​oid异常,并重新加载应用程序 [英] Catch all possible android exception globally and reload application

查看:133
本文介绍了全球范围内捕捉一切可能的Andr​​oid异常,并重新加载应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道prevent系统崩溃最好的办法是在追赶不同的方法的所有可能的例外。所以我用的try catch块每一个地方在我的code。但是你知道有时候你忘记测试某些情况下这会导致一些unhanded异常和用户得到不幸的是应用程序停止工作......消息。这是坏的任何应用程序。不幸的是谁将会使用我的应用程序的人不是以英语为母语,所以他们不会太懂崩溃的消息。

I know the best way to prevent system crashes is catching all possible exception in different methods. So I use try catch blocks every where in my code. However as you know sometimes you forget to test some scenarios which cause some unhanded exceptions and a user gets "Unfortunately App stopped working..." message. This is bad for any application. Unfortunately the people who will use my app are not native English, so they will not understand crash message too.

所以,我想知道的是它可以捕获所有可能的例外全球(在一些主要的类不是所有的类和方法!只有一个try catch块)和重新加载应用程序自动并没有任何奇怪的消息?或者说,至少是有可能的更改崩溃信息

So I want to know is it possible to catch all possible exception globally ( with just one try catch block in some main classes not all classes and methods!!!) and reload application automatically and without any weird messages? Or at least is it possible to change the crash message?

坦克。

推荐答案

在您的onCreate

In your onCreate

Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread paramThread, Throwable paramThrowable) {
                //Catch your exception
                // Without System.exit() this will not work.
                System.exit(2);
            }
        });

这篇关于全球范围内捕捉一切可能的Andr​​oid异常,并重新加载应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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