android - 在 Thread 捕获 UncaughtException 之前上下文会发生什么

标签 android exception error-handling android-context

结合a previous question 我的,我想知道当异常被 Thread.UncaughtExceptionListener 捕获时,上下文到底发生了什么。当监听器接收到未捕获的异常时,上下文似乎陷入了困境。它可以访问它的一些方法(主要是与资源有关的方法),但是很多方法不起作用(或明显不起作用)。这包括(但不限于,我确定还有更多我没有找到):

  • Toast(如我的链接)
  • 对话框
  • 创建新 Activity 。这包括正常的 Context.startActivity(Intent)PendingIntent

然而,上下文仍然可以访问上下文资源(getString()Resources 和奇怪的 Notification)。

这是为什么?是什么导致上下文中的这种状态不平衡(?)阻止任何上下文驱动的调用?

下面我为您创建了一个测试应用程序,假设在未捕获异常时启动错误 Activity 。要在我一直在做的情况下测试我提到的事情( toast 和对话),将它们的构建器放在 BoomMitActivity 中代替通知构建器。

在给定的示例中, Activity 出错(怎么可能不出错?)并在单击 Notification 时启动 BoomMitActivtiy。但是,从未调用 BoomMit 的 onCreate

应用:

public class AndroidTestoActivity extends Activity implements UncaughtExceptionHandler {
    @Override public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        Thread.setDefaultUncaughtExceptionHandler(this);

        throw new RuntimeException("HAHAHAHA, I broke you");
    }

    @Override public void uncaughtException(Thread arg0, Throwable arg1) {
        finish();
        NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        Notification note = new Notification(R.drawable.ic_launcher, "Boom!", System.currentTimeMillis());

        Intent i = new Intent(this, BoomMitActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent pi = PendingIntent.getActivity(this, 1, i, PendingIntent.FLAG_ONE_SHOT);
        note.setLatestEventInfo(this, "Boom!", "Open BoomMitActivity", pi);
        note.flags |= Notification.FLAG_AUTO_CANCEL;

        nm.notify(1, note);
        Log.d("TAG", "End");
    }

    static class BoomMitActivity extends Activity {
        @Override public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            TextView tv = new TextView(this);
            tv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
            tv.setBackgroundColor(0xffff0000);
            tv.setText("BoomMitActivity is the one true activity");
            tv.setTextColor(0xff00ffff);
            setContentView(tv);
        }
    }
}

list :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.testo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".AndroidTestoActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name="AndroidTestoActivity$BoomMitActivity"
            android:label="I'm a real boy!"
            android:taskAffinity=""
            android:excludeFromRecents="true"
            android:launchMode="singleTask">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
</manifest>

最佳答案

Context 本身没有任何变化。如果你说的是当你处于应用程序崩溃对话框出现的情况时,那么崩溃的线程就坐在那里等待用户按下一个按钮并让它继续自杀。此时系统知道你的应用程序已被删除,只是等待用户确认并最终终止它。所以你真的应该假设你正在出路,而不是依赖任何有效的东西。

此外,如果这次崩溃来自主线程,那么该线程现在正处于阻塞状态,等待用户确认崩溃对话框,以便它可以自杀。它不是坐在那里处理它的消息循环,而是坐在那里完全阻塞等待自杀。不会执行计划在该线程上的任何工作。对于主线程,这包括对 Activity.onCreate()、Service.onStart() 等组件的回调。对于任何线程,这包括为与该线程关联的窗口 UI 分派(dispatch)任何工作。同样,如果这是主线程,这可能意味着您的所有 UI。

关于android - 在 Thread 捕获 UncaughtException 之前上下文会发生什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10485001/

相关文章:

c# - 具有多个异常的 UnitTest ExpectedException

javascript - 如何正确尝试并捕获jQuery

c# - 检查 LINQ 查询中的 Var 是否为 Null 并返回早于 x 的值

android - 如何在 android 中使用 facebook sdk 在墙上的帖子中标记 friend ?

android - YouTube watchme 安卓应用异常找不到 "libffmpeg.so"

android - 在 Titanium 中,如何让视频在 Android 中播放?

c# - 如何检查异步 Web 服务调用的错误

android - 如何在Android中将网格子项的绘制方向更改为Right_To_Left

java - 安装新的 tomcat 服务器 (5.5) 时出现错误消息

java - 如何在 Java 中管理复杂的流程控制?