如何阻止我的应用程序从zombifying后我处理未捕获的Excepition?我的、应用程序、Excepition、zombifying

2023-09-06 09:45:15 作者:心比天高 命比纸薄

我处理,它通过发生在我的应用程序的任何未捕获的异常:

  Thread.setDefaultUncaughtExceptionHandler(本);
 

人,这是我的发射活动实施UncaughtExceptionListener。我处理异常和关闭就好了发到我的日志服务器,但是,我的应用程序将不会结束。它只是沿着运行作为一个僵尸,直到家庭或后退按钮是pressed。我怎样才能杀死进程异常处理后?

修改 下面是测试和工作的活动,抛出一个未捕获的异常是被抓(由线程),并进行处理。敬酒从不公布,并处理转到僵尸一旦异常记录。

 类TestActivity扩展活动实现UncaughtExceptionListener {

    @覆盖公共无效的onCreate(包冰柱){
        super.onCreate(冰柱);

        Thread.setDefaultUncaughtExceptionHandler(本);
        抛出新的RuntimeException(我是一个destuctive鼻屎。);

        的setContentView(R.layout.activity_test);
    }

    @覆盖公共无效uncaughtException(线程的线程,可抛出折腾){
        Log.e(TAG,未捕获的异常已:收到,折腾);
        Toast.makeText(这个大错误,Toast.LENGTH_LONG).show();
    }
}
 
应用程序已被java安全阻止

解决方案

我做了一些测试与你的榜样code和发现完成()将工作从 uncaughtException()。这是无声的但这么不给任何反馈给用户,就像你,我无法调用吐司或任何其他视觉消息框,即使使用 getApplicationContext()

发送任何反馈意见,我是能够得到工作的唯一方法是使用 NotificationManager 来发送一个状态栏通知使用 getApplicationContext()上下文

我没花多少时间就可以了,但遗憾的是,似乎这并不意味着必须提供一个 PendingIntent 当用户点击该通知自己来处理。具有相同活动这样做能显着风险无限循环,如果活动的设置 PendingIntent 是一个产生未处理的异常。

这将是可能的,但是,建立一个基本的变化活动也许这将是打开的,并解释了崩溃。对话框主题

I am handling any uncaught exceptions that occur in my application via:

Thread.setDefaultUncaughtExceptionHandler(this);

were this is my launcher Activity implementing UncaughtExceptionListener. I handle the exception and send it off to my log server just fine, however, my application then doesn't end. It just runs along as a zombie until the home or back button is pressed. How can I kill the process after handling the exception?

Edit Here is a test and working activity that throws an uncaught exception that is to be caught (by Thread) and handled. The toast is never posted and the process goes zombie once the exception is logged.

class TestActivity extends Activity implements UncaughtExceptionListener {

    @Override public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        Thread.setDefaultUncaughtExceptionHandler(this);
        throw new RuntimeException("I'm a destuctive booger.");

        setContentView(R.layout.activity_test);
    }

    @Override public void uncaughtException(Thread thread, Throwable toss) {
        Log.e(TAG, "An uncaught exception has been recieved.", toss);
        Toast.makeText(this, "Big Error", Toast.LENGTH_LONG).show();
    }
}

解决方案

I did some testing with your example code and discovered that finish() will work from uncaughtException(). It's silent however so doesn't give any feedback to the user and, like you, I was unable to invoke a Toast or any other visual message box even using getApplicationContext().

The only way of sending any feedback that I was able to get working was to use NotificationManager to send a status bar Notification using getApplicationContext() for the Context.

I didn't spend much time on it but unfortunately it seems this does mean having to provide a PendingIntent to handle when a user clicks on the notification itself. Doing this with the same Activity could obviously risk an infinite loop if the Activity set for the PendingIntent is one which generates an unhandled exception.

It would be possible, however, to create a vary basic Activity perhaps with a dialog theme that would be opened and explain the crash.