是否有可能建立某种形式的机器人全局异常处理的?有可能、全局、机器人、异常

2023-09-05 04:31:59 作者:我是逗比我比你逗

我的应用包括一系列活动的,用户必须通过其进行以线性方式。比方说,这一系列的活动,看起来是这样的:A(重presents主菜单),B,C,D,E的用户继续像这样:A - > B - >ç - >ð - >电子在每一个这些活动中,用户必须或者输入数据或允许设备自动获得的数据(例如,通过网络或蓝牙)。

My application includes a series of Activities, through which the user must proceed in a linear fashion. Let's say that this series of activities looks like this: A (represents the main menu), B, C, D, E. The user proceeds like this: A -> B -> C -> D -> E. In each of these activities, the user must either input data or allow the device to get the data automatically (e.g. via the network or Bluetooth).

有时候,我在中间活动之一的应用程序崩溃。什么结束了发生的事情,通常是应用程序移回活动或两种。例如,如果我在的活动Ð应用程序崩溃,应用程序可能会搬回到活动C或B.但问题是,这样的崩溃后,输入数据是在这样一个奇怪的状态,该应用程序再次崩溃,并显示力量关闭对话框,所有的方式回到到活动A,主菜单。

Occasionally, my app crashes in one of the middle activities. What ends up happening, typically, is that the app moves back an activity or two. For example, if my app crashes in Activity D, the app might move back to Activity C or B. But the problem is, after such a crash, the input data is in such a weird state that the app again crashes and shows the force close dialog, all the way back to Activity A, the main menu.

我如何能捕获这些异常是导致这些事故在全球整个应用程序,这样我就可以清理数据,并优雅地允许用户返回到主菜单?

How can I catch these exceptions that cause these crashes globally throughout the application, so I can clean up the data and gracefully allow the user to return to the main menu?

推荐答案

扩展应用程序类

import android.app.Application;
import android.util.Log;

public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {

            @Override
            public void uncaughtException(Thread thread, Throwable ex) {
                Log.e("MyApplication", ex.getMessage());            

            }
        });
    }
}

添加以下行的Andr​​oidManifest.xml文件的应用程序的属性。

Add the following line in AndroidManifest.xml file as Application attribute

android:name=".MyApplication"