由于Android手机型号,厂商等很多,我们不能保证测试时候没问题的程序,在各种手机上都没问题。
当出现问题,程序崩溃时,我们可以设置对应的监听,将对应的报错信息记录下来,上传至服务器。
一、原理
在Thread类中,有一个接口 UncaughtExceptionHandler
/** * Implemented by objects that want to handle cases where a thread is being * terminated by an uncaught exception. Upon such termination, the handler * is notified of the terminating thread and causal exception. If there is * no explicit handler set then the thread's group is the default handler. */ public static interface UncaughtExceptionHandler { /** * The thread is being terminated by an uncaught exception. Further * exceptions thrown in this method are prevent the remainder of the * method from executing, but are otherwise ignored. * * @param thread the thread that has an uncaught exception * @param ex the exception that was thrown */ void uncaughtException(Thread thread, Throwable ex); }
同时有一个实现该接口的成员变量 defaultUncaughtHandler
/** * Holds the default handler for uncaught exceptions, in case there is one. */ private static UncaughtExceptionHandler defaultUncaughtHandler;当程序出现未处理的异常(崩溃)时,会调用该成员变量的 uncaughtException 方法 ,因此,我们可以在此方法中进行记录异常信息,上传服务器等操作
二、使用方法
1.写一个 UncaughtExceptionHandler 的实现类 CrashHandler
package com.czlaite.utils; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.lang.Thread.UncaughtExceptionHandler; import java.text.SimpleDateFormat; import java.util.Date; import android.content.Context; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.os.Build; import android.os.Environment; import android.util.Log; import android.os.Process; public class CrashHandler implements UncaughtExceptionHandler{ private static final String TAG = "CrashHandler"; private static final boolean DEBUG = true; //路径,文件名前缀,后缀 private static final String PATH = Environment.getExternalStorageDirectory().getPath() + "/CrashTest/log/"; private static final String FILE_NAME_PERFIX = "crash"; private static final String FILE_NAME_SUFFIX = ".trace"; //系统默认的异常处理器 private UncaughtExceptionHandler mDefaultCrashHandler; private Context mContext; //单例模式 private static CrashHandler sInstance = new CrashHandler(); private CrashHandler() { } public static CrashHandler getInstance() { return sInstance; } public void init(Context context) { mDefaultCrashHandler = Thread.getDefaultUncaughtExceptionHandler(); Thread.setDefaultUncaughtExceptionHandler(this); mContext = context.getApplicationContext(); } /** * 这个是最关键的函数,当程序中有未被捕获的异常,系统将会自动调用#uncaughtException方法 * thread为出现未捕获异常的线程,ex为未捕获的异常,有了这个ex,我们就可以得到异常信息。 */ @Override public void uncaughtException(Thread thread, Throwable ex) { try { //导出异常信息到SD卡中 dumpExceptionToSDCard(ex); //这里可以通过网络上传异常信息到服务器,便于开发人员分析日志从而解决bug uploadExceptionToServer(); } catch (IOException e) { e.printStackTrace(); } ex.printStackTrace(); //如果系统提供了默认的异常处理器,则交给系统去结束我们的程序,否则就由我们自己结束自己 if (mDefaultCrashHandler != null) { mDefaultCrashHandler.uncaughtException(thread, ex); } else { Process.killProcess(Process.myPid()); } } /** * 将异常信息存到SD卡中 * @param ex * @throws IOException */ private void dumpExceptionToSDCard(Throwable ex) throws IOException { //如果SD卡不存在或无法使用,则无法把异常信息写入SD卡 if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { if (DEBUG) { Log.w(TAG, "sdcard unmounted,skip dump exception"); return; } } File dir = new File(PATH); if (!dir.exists()) { dir.mkdirs(); } long current = System.currentTimeMillis(); String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(current)); File file = new File(PATH + FILE_NAME_PERFIX + time + FILE_NAME_SUFFIX); try { PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file))); pw.println(time); dumpPhoneInfo(pw); pw.println(); ex.printStackTrace(pw); pw.close(); } catch (Exception e) { Log.e(TAG, "dump crash info failed"); } } /** * 记录手机对应的 信息 * @param pw * @throws NameNotFoundException */ 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); //android版本号 pw.print("OS 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); //cpu架构 pw.print("CPU ABI: "); pw.println(Build.CPU_ABI); } private void uploadExceptionToServer() { //TODO 将异常信息上传至服务器 } }
2. 在Application初始化的时候,为线程设置CrashHandler
package com.czlaite.application; import com.czlaite.utils.CrashHandler; import android.app.Application; import android.content.Context; public class MyApplication extends Application{ public static Context context; public static Context getContext(){ return context; } @Override public void onCreate() { super.onCreate(); context = getApplicationContext(); //为应用设置异常处理器 CrashHandler crashHandler = CrashHandler.getInstance(); crashHandler.init(context); } }
顺便给大家推荐一位大神,任玉刚 。http://my.csdn.net/singwhatiwanna