2
내 응용 프로그램의 충돌 보고서를 추적하고 메일을 보내려면 Uncaught 예외 처리기를 구현하고 있습니다. 그러나 예외 예외 처리기에서 메일을 보낼 수 없습니다. 예외 처리기 이름은 TopExceptionHandler입니다. TopExceptionHandler.java에서이 코드를 사용하고 있습니다.Uncaught Excepetion Handler에서 이메일을 보내는 방법은 무엇입니까?
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 [email protected]: "+
"\n\n"+
report+
"\n\n";
sendIntent.putExtra(Intent.EXTRA_EMAIL,
new String[] {"[email protected]"});
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();
}
}
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);
}
}
}
무엇이 문제입니까? – njzk2
@i는 이메일을 보낼 수 없습니다. 왜 이메일을 보낼 수 없습니까? – John
이것은 다소 모호합니다. 네가 이걸 실행할 때 어떻게 될지 짐작하지 마라. "작동하지 않는다"는 단서가 있습니까? 스택 추적은 어딘가에 있습니까? – njzk2