为了将未捕获异常的堆栈跟踪包含在Google Analytics中,我使用自定义的 ExceptionParser 替换了默认的 ExceptionParser。
现在,我可以得到带有堆栈跟踪的异常信息,同时也可以得到不带堆栈跟踪的异常信息(默认情况下)。
请问我做错了什么?
public class AnalyticsHelper
{
/**
* This method is called in my Application#onCreate(..)
**/
public static void initAnalytics(Context c)
{
GoogleAnalytics analytics = GoogleAnalytics.getInstance(c);
analytics.setAppOptOut(PreferenceManager.getDefaultSharedPreferences(c).getBoolean(PreferencesActivity.KEY_ANAYLTICS_OPT_OUT, false));
// setting uncaught exception handler
ExceptionReporter reporter = new ExceptionReporter(analytics.getInstance(c).newTracker(R.xml.tracker), Thread.getDefaultUncaughtExceptionHandler(), c);
reporter.setExceptionParser(new ExceptionParser()
{
@Override
public String getDescription(String p_thread, Throwable throwable)
{
StringWriter trace = new StringWriter();
throwable.printStackTrace(new PrintWriter(trace));
String stacktrace = trace.toString();
String message = (throwable.getMessage() == null) ? "no message" : throwable.getMessage();
return "Message: " + message
+ ", Stacktrace: " + stacktrace;
}
});
Thread.setDefaultUncaughtExceptionHandler(reporter);
}
}
tracker.xml
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
tools:ignore="TypographyDashes">
<integer name="ga_sessionTimeout">300</integer>
<bool name="ga_reportUncaughtExceptions">true</bool>
<bool name="ga_autoActivityTracking">false</bool>
<integer name="ga_dispatchPeriod">5</integer>
<string name="ga_trackingId">XXXXXX</string>
</resources>
ga_reportUncaughtExceptions
的行为吗?我已经在答案中添加了文档的参考链接。 - goofyz