a previous question 私の場合、例外がThread.UncaughtExceptionListener
でキャッチされたときの状況を正確に知りたいと思います。キャッチされない例外がリスナーによって受信されると、コンテキストはlimbo状態になっているように見えます。それは、いくつかのメソッド(主にリソースに関するもの)にアクセスできますが、多くの人が機能しません(または目に見えて動作しません)。新たな活動の スレッドがUncaughtExceptionをキャッチする前にコンテキストに何が起こるか
Toast
(私のリンクのように)Dialog
- 作成:これは(私はもっと私が発見していないがあると確信しているが、これらに限定されない)が含まれます。これには通常の
Context.startActivity(Intent)
とPendingIntent
が含まれます。コンテキストがまだコンテキストのリソース(getString()
、Resources
と奇妙なNotification
秒)に明らかなアクセス権を持っているしかし
。
これはなぜですか?このコンテキストの状態の不均衡(?)は、コンテキスト駆動型呼び出しを防止するために何が原因ですか?
以下は、例外がキャッチされないままになったときにエラーアクティビティを開始すると思われるテストアプリケーションを作成しました。私がやっている状況で、私が言及したこと(トーストとダイアログ)をテストするには、ビルダーを通知ビルダーの代わりにBoomMitActivityに配置してください。
この例では、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);
}
}
}
マニフェスト:
<?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>
なぜ 'BoomMitActivity'を' static'として取得しましたか? – Ronnie
@ userSeven7sネストしたアクティビティを静的でないと宣言することはできません。私はそれを自分のクラスに簡単に置くことができましたが、テストのためにAndroidTestoActivityに入れておくほうが簡単でした。 – AedonEtLIRA
普通のクラスでも同じですか? – Ronnie