Android Capturing Log in a file in application when application is force closed
In Android programming, I would like to understand how to log the "log data" into a file when my application encounters a force close. My current requirement is when the app force closes , the log data needs to be written in a file which could then be sent from the application(uses email intent) when the user uses the application again. I m开发者_开发问答 unable to understand which event or method which could be called as the moment force close happens, my application is crashed and i m out of my application code. Any sample program would be highly welcome.
Your best bet is to catch the errors in a try/catch, then use Log.getStackTraceString on the error. You are aware you get all this information through the Market, right?
Assuming you just want to check the crash log and this is not an inherent behavior I would rather use the existing infrastructure.
I believe you should not be doing it, unless it is in testing phase. Android Market provides the user an opportunity to report force closes and sends you the actual log. In the testing phase you can ask your testers to use applications that sends the log file, search for them in the market.
when your application starts for the first time try to register uncaught exception handler, this method will be called whenever there is an excpetion in your code.
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
Log.e("UNCAUGHT EXCEPTION", thread.toString());
Log.e("UNCAUGHT EXCEPTION", ex);
}
});
精彩评论