Android崩潰捕獲

來源:互聯網
上載者:User

標籤:異常處理   application   exception   android   crash   

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



Android崩潰捕獲

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.