Java tutorial
//package com.java2s; /******************************************************************* * Copyright (c) 2010 Rohit Kumbhar * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, * copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following * conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. *******************************************************************/ import java.io.PrintWriter; import java.io.StringWriter; import java.util.Date; import android.content.Context; import android.content.pm.PackageInfo; import android.content.pm.PackageManager.NameNotFoundException; public class Main { private static String getCrashReport(Throwable e, Context application) { StringBuffer body = new StringBuffer(); body.append("Timestamp: " + new Date().toString()); body.append("\n** Crash Report **\n"); try { PackageInfo pi = application.getPackageManager().getPackageInfo(application.getPackageName(), 0); body.append("Package Name: ").append(pi.packageName).append("\n"); body.append("Package Version: ").append(pi.versionCode).append("\n"); body.append("Phone Model: ").append(android.os.Build.MODEL).append("\n"); body.append("Phone Manufacturer: ").append(android.os.Build.MANUFACTURER).append("\n"); body.append("Android Version:").append(android.os.Build.VERSION.RELEASE).append("\n"); } catch (NameNotFoundException e1) { } StringWriter stack = new StringWriter(); PrintWriter writer = new PrintWriter(stack); e.printStackTrace(writer); body.append("\n\nStacktrace:\n\n"); body.append(stack.toString()).append("\n"); if (e.getCause() != null) { Throwable cause = e.getCause(); stack = new StringWriter(); writer = new PrintWriter(stack); cause.printStackTrace(writer); body.append("\n\nCause Stacktrace:\n\n"); body.append(stack.toString()).append("\n"); } body.append("** Crash Report **\n"); return body.toString(); } }