全局crash捕捉并上传的服务器 发表于 2017-04-18 | 分类于 Android 全局crash捕捉并上传的服务器 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134public class CrashHandler implements UncaughtExceptionHandler { public static final String TAG = "CrashHandler"; public static final boolean DEBUG = false; public static String PATH; private static final String FILE_NAME = "crash"; private static final String FILE_NAME_SUFFIX = ".log"; private static CrashHandler sInstance = new CrashHandler(); private UncaughtExceptionHandler mDefaultCrashHandler; private Context mContext; private HttpUtils httpUtils; private CrashHandler() { } public static CrashHandler getInstance() { return sInstance; } public void init(Context context) { httpUtils = new HttpUtils(); mDefaultCrashHandler = Thread.getDefaultUncaughtExceptionHandler(); Thread.setDefaultUncaughtExceptionHandler(this); mContext = context.getApplicationContext(); } @Override public void uncaughtException(Thread thread, Throwable ex) { try { boolean sdCardExist = Environment.getExternalStorageState() .equals(android.os.Environment.MEDIA_MOUNTED); //判断sd卡是否存在 if (sdCardExist) { PATH = mContext.getExternalCacheDir().getAbsolutePath() + File.separator + "CrashText" + File.separator + "log" + File.separator; } else { PATH = mContext.getFilesDir().getAbsolutePath() + File.separator + "CrashText" + File.separator + "log" + File.separator; } //导出到sdCard AND 上传至服务器 dumpException2SDCARD(ex); ex.printStackTrace(); if (!handleException(ex) && mDefaultCrashHandler != null) { mDefaultCrashHandler.uncaughtException(thread, ex); } else { try { Thread.sleep(3000); } catch (InterruptedException e) { Log.e(TAG, "error : ", e); } BaseActivity.exit(); } } catch (IOException e) { e.printStackTrace(); } } private boolean handleException(Throwable ex) throws IOException { if (ex == null) { return false; } new Thread(new Runnable() { @Override public void run() { Looper.prepare(); Toast.makeText(mContext, "很抱歉,程序出现异常,即将退出.", Toast.LENGTH_SHORT).show(); Looper.loop(); } }).start(); return true; } private void dumpException2SDCARD(Throwable ex) throws IOException { long current = System.currentTimeMillis(); String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(current)); File file = new File(PATH); if (!file.exists()) { file.mkdirs(); } File file1 = new File(PATH + FILE_NAME + time + FILE_NAME + FILE_NAME_SUFFIX); if (!file1.exists()) { file1.createNewFile(); } try { PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file1))); pw.println(time); dumpPhoneInfo(pw); pw.println(); ex.printStackTrace(pw); pw.close(); RequestParams params = new RequestParams(); params.addBodyParameter("file", file1); httpUtils.send(HttpRequest.HttpMethod.POST, Constant.UPLOADLOG, params, new RequestCallBack<String>() { @Override public void onSuccess(ResponseInfo<String> responseInfo) { Log.i("tag", "======success"); } @Override public void onFailure(HttpException error, String msg) { } }); Log.i(TAG, "===========" + file1.getAbsolutePath()); } catch (Exception e) { Log.e(TAG, "写入失败"); } } private void dumpPhoneInfo(PrintWriter pw) throws NameNotFoundException { PackageManager pm = mContext.getPackageManager(); PackageInfo pi = pm.getPackageInfo(mContext.getPackageName(), PackageManager.GET_ACTIVITIES); pw.print("APP Version:"); pw.print(pi.versionName); pw.print("_"); pw.println(pi.versionCode); pw.print("Android Version:"); pw.print(Build.VERSION.RELEASE); pw.print("_"); pw.println(Build.VERSION.SDK_INT); pw.print("Vendor:"); pw.println(Build.MANUFACTURER); pw.print("Model:"); pw.println(Build.MODEL); pw.print("CPU ABI"); pw.println(Build.CPU_ABI); }} 然后在application里初始化 123456789101112131415public class Application extends android.app.Application { private static Application sInstance; @Override public void onCreate() { super.onCreate(); sInstance = this; CrashHandler crashHandler = CrashHandler.getInstance(); crashHandler.init(this); } public static Application getInstance() { return sInstance; }}