Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 调用“uncaughtException”时关闭所有打开的活动_Android_Unhandled Exception_Uncaught Exception - Fatal编程技术网

Android 调用“uncaughtException”时关闭所有打开的活动

Android 调用“uncaughtException”时关闭所有打开的活动,android,unhandled-exception,uncaught-exception,Android,Unhandled Exception,Uncaught Exception,我想做的是,当出现任何未处理的异常时,关闭所有打开的活动。 我有一个BaseActivity,它是我应用程序中所有活动以及从该类派生的每个活动的基础。 我在BaseActivity的onCreate中调用了setDefaultUncaughtExceptionHandler 我有一个基本活动,如下所示: public abstract class BaseActivity extends Activity { @Override public void onCreate(Bun

我想做的是,当出现任何未处理的异常时,关闭所有打开的活动。
我有一个
BaseActivity
,它是我应用程序中所有活动以及从该类派生的每个活动的基础。
我在
BaseActivity
onCreate
中调用了
setDefaultUncaughtExceptionHandler

我有一个
基本活动
,如下所示:

public abstract class BaseActivity extends Activity 
{
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        // Exceptions that are not handled are received in GlobalExceptionHandler class              
        Thread.setDefaultUncaughtExceptionHandler(new MyExceptionHandler());
    }

    public class MyExceptionHandler implements UncaughtExceptionHandler 
    {  
        @Override  
        public void uncaughtException(Thread thread, Throwable e) 
        {
            String report = "";

            // Code to generate report string
            ......
            // Code to generate report string

            // Start error screen 
            Intent intent = new Intent(mContext, ErrorScreen.class);
            intent.putExtra("error", report);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);

            // Exit
            System.exit(0);
        } 
    }
}
以下是我的ErrorScreen活动:

public class ErrorScreen extends Activity implements OnClickListener
{
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.error_screen_layout);

        // Display error report from intent
    }

    @Override
    public void onClick(View view) 
    {
        switch(view.getId())
        {
            case R.id.btn_exit_app:
            {
                // All this is not working
                // and few activities are still there 
                finish();

                android.os.Process.killProcess(android.os.Process.myPid());

                System.exit(1);
            }
            break;
        }
    }
}
在任何未处理的异常情况下,将调用my
uncaughtException
函数(如预期),然后启动新活动以显示错误详细信息,并在
ErrorScreen
活动中单击一个按钮退出

但问题是,选择退出应用程序按钮后,应用程序中的活动并不是全部关闭,仍然有少数活动处于打开状态,如何关闭所有打开的活动

private Thread.UncaughtExceptionHandler androidDefaultUEH;

private Thread.UncaughtExceptionHandler handler = new Thread.UncaughtExceptionHandler() {
    public void uncaughtException(Thread thread, Throwable ex) {
        //mInstance.startActivity(new Intent(mInstance, HomeActivity.class));
        Log.e("TestApplication", "Uncaught exception is: ", ex);
        // here I do logging of exception to a db
        Toast.makeText(getApplicationContext(), "Something went wrong..... TimesNow App is restarting...", Toast.LENGTH_SHORT).show();

        PendingIntent myActivity = PendingIntent.getActivity(getApplicationContext(),
                192837, new Intent(getApplicationContext(), ToBeOpenedActivity.class),
                PendingIntent.FLAG_ONE_SHOT);

        AlarmManager alarmManager;
        alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                15000, myActivity);
        System.exit(0);

        androidDefaultUEH.uncaughtException(thread, ex);
    }
};

在应用程序类中编写此方法

但我不想在15000毫秒后自动重启我的应用程序,也不想只显示祝酒词。我只想结束所有的公开活动。就是这样。意味着你想关闭应用程序??是的,我只想显示错误信息,当用户选择退出应用程序按钮时,我想完全关闭应用程序。