简体   繁体   English

当应用程序在Android中崩溃时,如何禁用显示的警告框

[英]How disable alert box that appears ,while app crashes in Android

I want to handle the crashes smoothly ,for that i am restarting the app for every uncaught exceptions but there is always alert box is there "unfortunately ,xyz has stopped working" code that i am using to handle uncaught exceptions : 我想顺利处理崩溃,因为我正在为每个未捕获的异常重新启动应用程序,但总是有警告框“不幸的是,xyz已经停止工作”我用来处理未捕获的异常的代码:

intent = PendingIntent.getActivity(this.getApplication().getBaseContext(), 0,
                new Intent(getIntent()), getIntent().getFlags());

        Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread paramThread,
                    Throwable paramThrowable) {
                Log.e("Alert", "Lets See if it Works !!!");
        AlarmManager mgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 2000, intent);
                System.exit(0);

            }

When you get "unfortunately ,xyz has stopped working" , always put some breakpoints and DEBUG your code properly.Check your LOGCAT, you will land up finding the problems you are facing when your app crashes. 当你“不幸的是,xyz已停止工作”时,总是放置一些断点并正确调试你的代码。检查你的LOGCAT,当你的应用程序崩溃时,你会发现你遇到的问题。 Thanks! 谢谢!

Code: 码:

public class BaseActivity extends Activity {
    @Override
    public void onCreate() {
        super.onCreate();

        final Thread.UncaughtExceptionHandler oldHandler =
            Thread.getDefaultUncaughtExceptionHandler();

        Thread.setDefaultUncaughtExceptionHandler(
            new Thread.UncaughtExceptionHandler() {
                @Override
                public void uncaughtException(
                    Thread paramThread,
                    Throwable paramThrowable
                ) {
                    //Do your own error handling here

                    if (oldHandler != null)
                        oldHandler.uncaughtException(
                            paramThread,
                            paramThrowable
                        ); //Delegates to Android's error handling
                    else
                        System.exit(2); //Prevents the service/app from freezing
                }
            });
    }
}

The alert is done by the system, there isn't much you can do about it. 警报由系统完成,您无能为力。

You should just catch an Exception when it is thrown and then let the flow of your app continue accordingly. 您应该在抛出异常时捕获异常,然后让应用程序的流程继续相应。 Restarting your app every time an Exception occurs is anything but smooth. 每次发生异常时重新启动应用程序都是顺利的。

           @Override
           public void uncaughtException(Thread paramThread,
                           Throwable paramThrowable) {
                   Log.e("Alert", "Restarting app !!!");
                   Intent intent = getBaseContext().getPackageManager()
                        .getLaunchIntentForPackage( getBaseContext().getPac
           //i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
           intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTI
           startActivity(intent);
           System.exit(2);
           }
   });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM