0
今、私はこれを試しています。カメラサービスへの接続に失敗しただけで、アンドロイドでuncaughtExceptionを実行してください
MainActivity.java
Thread.setDefaultUncaughtExceptionHandler(new UncaughtException(MainActivity.this));
UncaughtException.class
public class UncaughtException implements Thread.UncaughtExceptionHandler {
private Context context;
private static Context context1;
public UncaughtException(Context ctx) {
context = ctx;
context1 = ctx;
}
public void uncaughtException(Thread t, Throwable e){
try {
StringBuilder report =new StringBuilder();
sendErrorMail(report);
} catch (Throwable ignore) {
Log.e(UncaughtException.class.getName(),
"Error while sending error e-mail", ignore);
}
}
public void sendErrorMail(final StringBuilder errorContent) {
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
new Thread() {
@Override
public void run() {
Looper.prepare();
builder.setTitle("sorry.");
builder.create();
builder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
System.exit(0);
}
});
builder.setMessage("OoOopps, Your application has crashed");
builder.show();
Looper.loop();
}
}.start();
}
}
この良い仕事。
しかし、これはすべてダイアログを表示する例外が発生します。
「カメラサービスへの接続に失敗しました」という例外が発生したときにダイアログを表示したい場合があります。
他の言葉では、私のデバイスがカメラに接続していない場合、ダイアログを表示しています。 (アプリの停止メッセージではない)。
ありがとうございますが、わかりません。あなたはカメラの例外をキャッチアドバイス? – chohyunwook
更新された回答を見る私はコードスニペット –
を追加しました。この場合、常にあなたが最初に知っている例外をキャッチする必要があります。 UngoughtExceptionHandlerは、あなたが忘れてしまった例外をキャッチするためにそこにある第二の安全ネットのようなものです。 –