获取崩溃日志并将其作为电子邮件发送 [英] Getting the crash log and send it as email
问题描述
从我的搜索中,我得到了以下用于获取崩溃日志的代码.
From my search, i got the below code for getting the Crash Log .
try {
Process process = Runtime.getRuntime().exec("logcat -d");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
StringBuilder log=new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null)
{
log.append(line);
}
但是我在哪里添加这段代码,这样每当我的应用程序崩溃时我就应该得到崩溃报告.
But where do i add this code, so that i should get the crash report whenever my app crashes .
我也想通过电子邮件发送或发送到服务器,但在应用程序崩溃后,如何调用操作发送电子邮件/HTTP post 方法.
Also i want to email it or send to the server, but after the app getting crashed, how to call the action to send email/HTTP post method .
请提前告知和感谢.
推荐答案
处理崩溃日志的最佳方法是创建一个 UncaughtExceptionHandler
并根据您的要求进行处理.创建一个 BaseActivity
类并用它扩展所有活动,并将此代码内容放入 BaseActivity
类中.
The best way to handle crash logs is creating an UncaughtExceptionHandler
and handling it as per your requirement. Create a BaseActivity
class and extend all the Activities with that and put this code stuff in the BaseActivity
class.
private Thread.UncaughtExceptionHandler handleAppCrash =
new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
Log.e("error", ex.toString());
//send email here
}
};
然后只需在 BaseActivity
的 onCreate()
方法中使用
Then just enable is inside onCreate()
method of your BaseActivity
by using
Thread.setDefaultUncaughtExceptionHandler(handleAppCrash);
因此,现在每当您的应用程序发生崩溃时,都会调用 uncaughtException()
并且您必须相应地处理崩溃.
So, now whenever there will be a crash in your Application uncaughtException()
will be called and you will have to handle the crash accordingly.
这篇关于获取崩溃日志并将其作为电子邮件发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!