全局捕获所有可能的android异常并重新加载应用程序 [英] Catch all possible android exception globally and reload application

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

问题描述

我知道防止系统崩溃的最好方法是用不同的方法捕获所有可能的异常.所以我在代码中的每个地方都使用 try catch 块.然而,正如您所知,有时您会忘记测试一些会导致一些未经处理的异常的场景,并且用户会收到不幸的是应用程序停止工作..."消息.这对任何应用程序都是不利的.不幸的是,将使用我的应用程序的人不是英语母语,因此他们也不会理解崩溃消息.

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

这篇关于全局捕获所有可能的android异常并重新加载应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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