gpt4 book ai didi

android - 如何从未捕获的异常处理程序发送电子邮件?

转载 作者:行者123 更新时间:2023-11-29 14:05:19 25 4
gpt4 key购买 nike

我正在实现未捕获异常处理程序来跟踪我的应用程序的崩溃报告并向我发送邮件。但是我无法从我的异常处理程序发送邮件。我的未捕获异常处理程序名称是 TopExceptionHandler。我在 TopExceptionHandler.java 中使用此代码

public class TopExceptionHandler implements Thread.UncaughtExceptionHandler
{
private Thread.UncaughtExceptionHandler defaultUEH;

private Activity app = null;

public TopExceptionHandler(Activity app)
{
this.defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
this.app = app;


}



@Override
public void uncaughtException(Thread t, Throwable e)
{

StackTraceElement[] arr = e.getStackTrace();
String report = e.toString()+"\n\n";
report += "--------- Stack trace ---------\n\n";
for (int i=0; i<arr.length; i++)
{
report += " "+arr[i].toString()+"\n";
}
report += "-------------------------------\n\n";

// If the exception was thrown in a background thread inside
// AsyncTask, then the actual exception can be found with getCause
report += "--------- Cause ---------\n\n";
Throwable cause = e.getCause();
if(cause != null) {
report += cause.toString() + "\n\n";
arr = cause.getStackTrace();
for (int i=0; i<arr.length; i++)
{
report += " "+arr[i].toString()+"\n";
}
}
report += "-------------------------------\n\n";

Log.v("report", report);

try {
FileOutputStream trace = app.openFileOutput(
"stack.trace", Context.MODE_PRIVATE);
trace.write(report.getBytes());
trace.close();
} catch(IOException ioe) {
// ...
}




sendEmail(report);




defaultUEH.uncaughtException(t, e);


}


private void sendEmail(String report)
{

try{
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
String subject = "Error report";
String body =
"Mail this to sivaraj@onederr.com: "+
"\n\n"+
report+
"\n\n";

sendIntent.putExtra(Intent.EXTRA_EMAIL,
new String[] {"sivaraj@onederr.com"});
sendIntent.putExtra(Intent.EXTRA_TEXT, report);
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "hai");
sendIntent.setType("message/rfc822");

app.startActivity(Intent.createChooser(sendIntent, "Email:"));

}
catch(Exception e)
{
Log.v("sendmail", e.toString());
}
}



@Override
protected void finalize() throws Throwable {
if (Thread.getDefaultUncaughtExceptionHandler().equals(this))
Thread.setDefaultUncaughtExceptionHandler(defaultUEH);
super.finalize();
}

CrashreportActivity.java

public class CrashreportActivity extends Activity {

protected TopExceptionHandler mDamageReport = new TopExceptionHandler(this);
String len=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Thread.setDefaultUncaughtExceptionHandler(new TopExceptionHandler(this));

for(int i=0;i<len.length();i++)
{
Log.v("error", ""+i);
}
}
}

最佳答案

当我尝试在 uncaughtException 的主体中做某事时,它总是失败。因此,我使用 AlarmManager 在应用程序关闭后引发 Activity 。通过这种方式,您可以发送一封电子邮件,或使用邮件内容和错误并重新启动通知重新激活您的应用程序。

关于android - 如何从未捕获的异常处理程序发送电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7385957/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com