編輯:關於Android編程
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);
}
@Override
public void uncaughtException(Thread thread, Throwable ex) {
GMLog.i(TAG, uncaughtException);
GMLog.i(TAG, ex.getLocalizedMessage());
ex.printStackTrace();
logStr = getStackTrace(ex);
// 使用 Toast 來顯示異常信息
new Thread() {
@Override
public 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 getCause
Throwable cause = th;
while (cause != null) {
cause.printStackTrace(printWriter);
cause = cause.getCause();
}
final String stacktraceAsString = result.toString();
printWriter.close();
return stacktraceAsString;
}
}
public class MyApplication extends Application {
ErrorReport errorReport;
@Override
public void onCreate() {
super.onCreate();
errorReport = new ErrorReport(this);
}
}
Android 5.0 Settings源碼簡要分析
概述:先聲明:本人工作快兩年了,仍是菜鳥級別的,慚愧啊!以前遇到好多知識點都沒有記錄下來,感覺挺可惜的,現在有機會接觸Android 源碼。我們一個Android組的搞S
Android小知識庫
The APK file does not exist on disk.Error while Installing APK解決方案:Android Studio獲取SH
Android應用程序窗口View的創建過程
View類是android中非常重要的一個類.view是應用程序界面的直觀體現,我們看到的應用程序界面就可以看作是View(視圖)組成的.那麼我們應用程序的界面是怎麼創建
Android AutoCompleteTextView自動提示文本框實例代碼
自動提示文本框(AutoCompleteTextView)可以加強用戶體驗,縮短用戶的輸入時間(百度的搜索框就是這個效果)。先給大家展示下效果圖,如果大家感覺