我希望我的 Android 应用程序通过使用(POST)Web 服务将堆栈跟踪发送到我的后端应用程序来处理未捕获的异常。为了做到这一点,我在我的父活动中设置了一个 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);
但我的网络服务没有联系。当我在 try 子句的第二行设置断点时,调试器似乎很短暂地到达了该行,然后程序和调试器就简单地退出了。我还尝试创建一个新线程来处理 web 服务调用,我尝试使用 AsyncTasks,但行为保持不变:从未联系过我的 web 服务。出了什么问题?
谢谢。