需要處理未處理的異常并發(fā)送日志文件當(dāng)我的應(yīng)用程序創(chuàng)建一個未處理的異常時,而不是簡單地終止,我想首先給用戶一個發(fā)送日志文件的機會。我意識到在得到隨機異常后做更多的工作是有風(fēng)險的,但是,最糟糕的是應(yīng)用程序已經(jīng)崩潰,日志文件沒有發(fā)送。這比我預(yù)期的要復(fù)雜得多:)工作原理:(1)捕獲未知異常,(2)提取日志信息并寫入文件。不起作用的是:(3)開始發(fā)送電子郵件的活動。最終,我還有另一個活動需要用戶的許可。如果我讓電子郵件活動正常工作,我不希望給對方帶來太大的麻煩。問題的癥結(jié)在于,未處理的異常在我的Application類中被捕獲。因為這不是一個活動,所以如何使用Intent.ACTION_Send啟動一個活動并不明顯。也就是說,通常啟動一個叫做startActivity并使用onActivityResult的活動。這些方法由活動支持,但不受應(yīng)用程序的支持。對如何做到這一點有什么建議嗎?下面是一些代碼片段作為開始指南:public class MyApplication extends Application{
defaultUncaughtHandler = Thread.getDefaultUncaughtExceptionHandler();
public void onCreate ()
{
Thread.setDefaultUncaughtExceptionHandler (new Thread.UncaughtExceptionHandler()
{
@Override
public void uncaughtException (Thread thread, Throwable e)
{
handleUncaughtException (thread, e);
}
});
}
private void handleUncaughtException (Thread thread, Throwable e)
{
String fullFileName = extractLogToFile(); // code not shown
// The following shows what I'd like, though it won't work like this.
Intent intent = new Intent (Intent.ACTION_SEND);
intent.setType ("plain/text");
intent.putExtra (Intent.EXTRA_EMAIL, new String[] {"me@mydomain.com"});
intent.putExtra (Intent.EXTRA_SUBJECT, "log file");
intent.putExtra (Intent.EXTRA_STREAM, Uri.parse ("file://" + fullFileName));
startActivityForResult (intent, ACTIVITY_REQUEST_SEND_LOG);
}
public void onActivityResult (int requestCode, int resultCode, Intent data)
{
if (requestCode == ACTIVITY_REQUEST_SEND_LOG)
System.exit(1);
}}
- 3 回答
- 0 關(guān)注
- 332 瀏覽
添加回答
舉報
0/150
提交
取消