(POST) Web サービスを使用してバックエンド アプリケーションにスタック トレースを送信することで、キャッチされない例外を Android アプリで処理したいと考えています。これを行うために、親アクティビティに UncaughtExceptionHandler を設定します。
Thread.setDefaultUncaughtExceptionHandler(new CustomExceptionHandler(this));
私の CustomExceptionHandler では、uncaughtException メソッドをオーバーライドします。
@Override
public void uncaughtException(Thread thread, Throwable ex) {
HttpURLConnection connection = null;
try {
URL url = new URL("http://mywebserviceurl.com?message=justatest");
connection = (HttpURLConnection) url.openConnection();
connection.setReadTimeout(5000);
connection.setConnectTimeout(5000);
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.connect();
} catch (Exception e) {
//Handle crash
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
これをテストするために、アプリを強制的にクラッシュさせます。
new Handler().postDelayed(new Runnable(){
@Override
public void run() {
throw new RuntimeException("");
}
}, 2500);
しかし、私のWebサービスには連絡がありません。try 句の 2 行目にブレークポイントを配置すると、デバッガーがその行に非常に短時間到着したように見え、その後、プログラムとデバッガーは単純に終了します。また、Web サービス呼び出しを処理する新しいスレッドを作成しようとしました。AsyncTasks を使用してみましたが、動作は同じままです。何がうまくいかないのですか?
ありがとう。