Android崩溃捕获
来源:互联网 发布:生产进度软件 编辑:程序博客网 时间:2024/05/21 10:32
public class ErrorReport implements Thread.UncaughtExceptionHandler{public static final String TAG = "GouMinErrorReport";private Context mContext;// 系统默认的 UncaughtException 处理类private Thread.UncaughtExceptionHandler mDefaultHandler;private String logStr;public ErrorReport(Context context) {mContext = context;// 获取系统默认的 UncaughtException 处理器mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();Thread.setDefaultUncaughtExceptionHandler(this);}@Overridepublic void uncaughtException(Thread thread, Throwable ex) {GMLog.i(TAG, "uncaughtException");GMLog.i(TAG, ex.getLocalizedMessage());ex.printStackTrace();logStr = getStackTrace(ex);// 使用 Toast 来显示异常信息new Thread() {@Overridepublic void run() {Looper.prepare();Toast.makeText(mContext, "很抱歉,程序出现异常,即将退出。", Toast.LENGTH_SHORT).show();sendReport(logStr);Looper.loop();}}.start();// 如果用户没有处理则让系统默认的异常处理器来处理if (mDefaultHandler != null) {mDefaultHandler.uncaughtException(thread, ex);}}private void sendReport(String stackTrace) {GMLog.i(TAG, "sendReport");HttpClient httpclient = new DefaultHttpClient();final HttpPost httppost = new HttpPost(mErrorReportReq.getUrl());try {httppost.setEntity(new StringEntity(stackTrace) ));} catch (UnsupportedEncodingException e1) {e1.printStackTrace();}HttpResponse httpResponse;try {httpResponse = httpclient.execute(httppost);final int statusCode = httpResponse.getStatusLine().getStatusCode();GMLog.i(TAG, "sendReport response statusCode is:" + statusCode);if (statusCode == HttpStatus.SC_OK) {String response = EntityUtils.toString(httpResponse.getEntity(), HTTP.UTF_8);GMLog.i(TAG, "sendReport response:" + response);}} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {GMLog.i(TAG, "sendReport finally");if (httpclient != null) {httpclient.getConnectionManager().shutdown();httpclient = null;}}}private String getStackTrace(Throwable th) {final Writer result = new StringWriter();final PrintWriter printWriter = new PrintWriter(result);// If the exception was thrown in a background thread inside// AsyncTask, then the actual exception can be found with getCauseThrowable cause = th;while (cause != null) {cause.printStackTrace(printWriter);cause = cause.getCause();}final String stacktraceAsString = result.toString();printWriter.close();return stacktraceAsString;}}
对于Android 异常退出,主要是用到了 接口Thread.UncaughtExceptionHandler
public class MyApplication extends Application {ErrorReport errorReport;@Overridepublic void onCreate() {super.onCreate();errorReport = new ErrorReport(this);}}
https://github.com/ACRA/acra
https://github.com/ACRA/acra/wiki/AdvancedUsage#data-encoding-formjson
http://blog.csdn.net/moruite/article/details/7397000
http://www.cnblogs.com/lee0oo0/archive/2012/11/28/2793052.html
1 0
- Android捕获崩溃异常
- Android崩溃捕获
- Android捕获崩溃异常
- Android程序崩溃捕获
- Android捕获崩溃异常
- Android UncaughtExceptionHandler捕获崩溃异常
- 捕获android程序崩溃日志
- android 程序崩溃捕获日志
- Android崩溃异常捕获方法
- Android崩溃异常捕获方法
- 利用Android UncaughtExceptionHandler捕获崩溃异常
- 利用Android UncaughtExceptionHandler捕获崩溃异常
- 利用Android UncaughtExceptionHandler捕获崩溃异常
- Android UncaughtExceptionHandler捕获线程崩溃异常
- 利用Android的UncaughtExceptionHandler捕获崩溃异常
- Android异常捕获防止崩溃弹框
- Android CrashHandler 自定义崩溃异常捕获
- 捕获android程序崩溃异常日志
- oracle把查出来列结果集用逗号隔开一行显示
- 重写ViewGroup注意事项
- C++ 虚函数表解析
- 16进制char 转换 string
- IOS_ViewController生命周期内各个函数的调用次序
- Android崩溃捕获
- iOS应用内部切换语言,不跟随系统
- HDU4064 Carcassonne(状态压缩DP)
- versions 不能提交.a文件
- 开放的力量—破解GitHub的发展密码
- UVA 11168 - Airport
- Implement a stack that pops out the mostfrequently added item. Stack supports 3 functions – push,
- 内核对象(VC_Win32) 挺好的~
- AJAX---jQuery的相关使用(一)