Android 代码崩溃异常收集整理

来源:互联网 发布:阿里云虚拟干嘛呢 编辑:程序博客网 时间:2024/06/05 16:11

一个健壮的APP应该能收集运行中所有的崩溃信息,并把这些信息发送到服务器给程序员分析。

我们也知道崩溃信息的收集我们可以使用try...catch...进行收集,但是作为一个APP程序而言,在每个界面,没个方法都添加一个try...catch是不可能的,这个时候我们需要的是一套统一的解决方案。

怎么做这个统一的方案呢,我们这个时候需要了解一个很重要的接口:UncaughtExceptionHandler,这个接口可以用来处理未捕获的异常,这个异常就是指我们没有try...catch..捕捉到的异常。

于是我们设计了一个CrashHandler类来实现UncaughtExceptionHandler的接口功能,并在其中添加一些个性化操作,具体代码如下:

import java.io.File;import java.io.FileOutputStream;import java.io.PrintWriter;import java.io.StringWriter;import java.io.Writer;import java.lang.Thread.UncaughtExceptionHandler;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import android.app.Activity;import android.app.ActivityManager;import android.app.ActivityManager.MemoryInfo;import android.content.Context;import android.content.SyncStatusObserver;import android.content.pm.ApplicationInfo;import android.content.pm.PackageInfo;import android.content.pm.PackageManager;import android.content.pm.PackageManager.NameNotFoundException;import android.net.ConnectivityManager;import android.net.NetworkInfo;import android.os.Environment;import android.os.Looper;import android.provider.Settings.Secure;import android.telephony.TelephonyManager;import android.text.TextUtils;import android.util.Log;import android.widget.Toast;import com.alibaba.fastjson.JSON;import com.infrastructure.activity.BaseActivity;import com.infrastructure.net.RequestCallback;import com.infrastructure.net.RequestParameter;import com.infrastructure.net.URLData;import com.youngheart.base.AppBaseActivity;import static com.youngheart.base.AppBaseActivity.*;/** * UncaughtException处理类,当程序发生Uncaught异常的时候, * 由该类来接管程序,并记录发送错误报告. * 需要在Application中注册,为了要在程序启动器就监控整个程序。 */public class CrashHandler implements UncaughtExceptionHandler {    public static final String TAG = "CrashHandler";    public static final String APP_CACHE_PATH =            Environment.getExternalStorageDirectory().getPath()                    + "/YoungHeart/crash/";    // 系统默认的UncaughtException处理类    private Thread.UncaughtExceptionHandler mDefaultHandler;    // CrashHandler实例    private static CrashHandler instance;    // 程序的Context对象    private Context context;    // 用来存储设备信息和异常信息    private Map<String, String> infos = new HashMap<String, String>();    // 用于格式化日期,作为日志文件名的一部分    private DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");    /** 保证只有一个CrashHandler实例 */    private CrashHandler() {    }    /** 获取CrashHandler实例 ,单例模式 */    public static CrashHandler getInstance() {        if (instance == null)            instance = new CrashHandler();        return instance;    }    /**     * 初始化     */    public void init(Context context) {        // 获取系统默认的UncaughtException处理器        mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();        // 设置该CrashHandler为程序的默认处理器        Thread.setDefaultUncaughtExceptionHandler(this);        this.context = context ;    }    /*     * 切换发生Crash所在的Activity     */    public void switchCrashActivity(Context context) {        this.context = context;    }    /**     * 当UncaughtException发生时会转入该函数来处理     */    @Override    public void uncaughtException(Thread thread, Throwable ex) {        if (!handleException(ex) && mDefaultHandler != null) {            // 如果用户没有处理则让系统默认的异常处理器来处理            mDefaultHandler.uncaughtException(thread, ex);        } else {            try {                Thread.sleep(3000);            } catch (InterruptedException e) {                Log.e(TAG, "error : ", e);            }            // 退出程序            android.os.Process.killProcess(android.os.Process.myPid());            System.exit(1);        }    }    /**     * 自定义错误处理,收集错误信息 发送错误报告等操作均在此完成.     *     * @param ex     * @return true:如果处理了该异常信息;否则返回false.     */    private boolean handleException(Throwable ex) {        if (ex == null) {            return false;        }        //把crash发送到服务器        sendCrashToServer(context, ex);        // 使用Toast来显示异常信息        new Thread() {            @Override            public void run() {                Looper.prepare();                Toast.makeText(context, "很抱歉,程序出现异常,即将退出.",                        Toast.LENGTH_SHORT).show();                Looper.loop();            }        }.start();        // 保存日志文件        saveCrachInfoInFile(ex);        return true;    }    /**     * 保存错误信息到文件中     *     * @param ex     * @return 返回文件名称,便于将文件传送到服务器     */    private String saveCrachInfoInFile(Throwable ex) {        StringBuffer sb = new StringBuffer();        for (Map.Entry<String, String> entry : infos.entrySet()) {            String key = entry.getKey();            String value = entry.getValue();            sb.append(key + "=" + value + "\n");        }        Writer writer = new StringWriter();        PrintWriter printWriter = new PrintWriter(writer);        ex.printStackTrace(printWriter);        Throwable cause = ex.getCause();        while (cause != null) {            cause.printStackTrace(printWriter);            cause = cause.getCause();        }        printWriter.close();        String result = writer.toString();        sb.append(result);        try {            long timestamp = System.currentTimeMillis();            String time = formatter.format(new Date());            String fileName = "crash-" + time + "-" + timestamp + ".log";            if (Environment.getExternalStorageState().equals(                    Environment.MEDIA_MOUNTED)) {                File dir = new File(APP_CACHE_PATH);                if (!dir.exists()) {                    dir.mkdirs();                }                FileOutputStream fos = new FileOutputStream(APP_CACHE_PATH + fileName);                fos.write(sb.toString().getBytes());                fos.close();            }            return fileName;        } catch (Exception e) {            Log.e(TAG, "an error occured while writing file...", e);        }        return null;    }    /**     * 收集程序崩溃的相关信息     *     * @param ctx     */    public void sendCrashToServer(final Context ctx, Throwable ex) {        //取出版本号        PackageManager pm = ctx.getPackageManager();        PackageInfo pi = null;        try {            pi = pm.getPackageInfo(ctx.getPackageName(),                    PackageManager.GET_ACTIVITIES);            if (pi != null) {                String versionName = pi.versionName == null ? "null"                        : pi.versionName;                String versionCode = pi.versionCode + "";                infos.put("versionName", versionName);                infos.put("versionCode", versionCode);            }        } catch (NameNotFoundException e1) {            e1.printStackTrace();        }        HashMap<String, String> exceptionInfo = new HashMap<String, String>();        try {            ActivityManager am = (ActivityManager) ctx                    .getSystemService(Context.ACTIVITY_SERVICE);            String pageName = ctx.getClass().getName();            MemoryInfo mi = new MemoryInfo();            am.getMemoryInfo(mi);            String memoryInfo = "Memory info:" + mi.availMem + ",app holds:"                    + mi.threshold + ",Low Memory:" + mi.lowMemory;            ApplicationInfo appInfo = ctx.getPackageManager()                    .getApplicationInfo(ctx.getPackageName(),                            PackageManager.GET_META_DATA);            String version = ctx.getPackageManager().getPackageInfo(                    ctx.getPackageName(), 0).versionName;            exceptionInfo.put("PageName", pageName);            exceptionInfo.put("ExceptionName", ex.getClass().getName());            exceptionInfo.put("ExceptionType", "1");            exceptionInfo.put("ExceptionsStackDetail", getStackTrace(ex));            exceptionInfo.put("AppVersion", version);            exceptionInfo.put("OSVersion",  android.os.Build.VERSION.RELEASE);            exceptionInfo.put("DeviceModel", android.os.Build.MODEL);            exceptionInfo.put("DeviceId", getDeviceID(ctx));            exceptionInfo.put("NetWorkType", String.valueOf(isWifi(context)));            exceptionInfo.put("ClientType", "100");            exceptionInfo.put("MemoryInfo", memoryInfo);            Iterator iter = exceptionInfo.entrySet().iterator();            while (iter.hasNext()) {                HashMap.Entry entry = (HashMap.Entry) iter.next();                Object key = entry.getKey();                Object val = entry.getValue();                System.out.println(key+"***********"+val);                }            final String rquestParam = JSON.toJSONString(exceptionInfo, true);            ArrayList<RequestParameter> params = new ArrayList<RequestParameter>();                    RequestParameter rp1 = new RequestParameter("exception", rquestParam);                    params.add(rp1);                    RemoteService.getInstance().invoke("sendException", params);        } catch (Exception e) {            e.printStackTrace();        }    }    /**     *     * @Title: getDeviceID     * @Description: 获取手机设备号     * @param context     * @return String     * @throws     */    public static final String getDeviceID(Context context) {        String deviceId = null;        try {            TelephonyManager tm = (TelephonyManager) context                    .getSystemService(Context.TELEPHONY_SERVICE);            deviceId = tm.getDeviceId();            tm = null;            if (TextUtils.isEmpty(deviceId)) {                deviceId = Secure.getString(context.getContentResolver(),                        Secure.ANDROID_ID);            }        } catch (Exception e) {            deviceId = Secure.getString(context.getContentResolver(),                    Secure.ANDROID_ID);        }        return deviceId;    }    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;    }    private static int isWifi(Context mContext) {        ConnectivityManager connectivityManager = (ConnectivityManager) mContext                .getSystemService(Context.CONNECTIVITY_SERVICE);        NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();        if (activeNetInfo != null                && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {            return 1;        }        return 0;    }}

这个函数主要实现下面几件事情:

  • 发送错误日志到服务器
  • 保存错误日志到本地
  • 提示用户,程序即将崩溃,进行友好提示。

我们发现这几个功能其实其中在handleException的方法中,在这里面有发送错误日志到服务器的函数sendCrashToServer,有友好提示,有保存日志到sd卡saveCrachInfoInFile方法。


我们只需要在Application中添加CrashHandler.getInstance().init(getApplicationContext());方法就可以获取到整个项目中的崩溃日志,并且把这些日志发送到我们的服务器中。

其实目前互联网上有很多异常的收集和整理的第三方平台,这些平台的好处就是他们提供了一整套的崩溃的分类和报表统计工具。腾讯的Bugly就是其中之一,而且他们还提供修改意见。

但是我们不想用第三方,想把崩溃信息上传到自己的数据库中,我们应该怎么整理我们获取到的日志呢。

  • 有很多重复的崩溃日志,我们要把这些重复的日志分成几种形式进行操作
  1. 有不同设备在不同时间发出来重复的崩溃日志,这时要检查APP是否只对某些机型或Android版本才会发生类似的问题。
  2. 在不同设备在一个时间段发出的重复的日志,这个时候要看服务器是否返回了脏数据而APP并没用用try...catch...捕捉到
  3. 有相同设备在很短的时间内频繁的发送了重复的崩溃日志,这个就是崩溃后的善后工作没做好导致的,崩溃后APP视图重启发生崩溃的那个组件,就会造成崩溃-重启的死循环。
  • 每个异常信息都包括,崩溃所对应的名称,崩溃的详细信息,我们找的时候尽量通过这些给崩溃信息分类,解决。
0 0
原创粉丝点击