发布于 2016-02-28 00:48:12 | 162 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


这篇文章主要为大家介绍了Android全局异常的捕获处理,为什么要进行捕获处理,如何进行捕获处理,想要了解的朋友可以参考一下

在Android开发中在所难免的会出现程序crash,俗称崩溃。用户的随意性访问出现测试时未知的Bug导致我们的程序crash,此时我们是无法直接获取的错误log的,也就无法修复Bug。这就会极大的影响用户体验,此时我们需要注册一个功能来捕获全局的异常信息,当程序出现crash信息,我们把错误log记录下来,上传到服务器,以便于我们能及时修复bug。实现这个功能我们需要依赖于UncaughtExceptionHandler这个类,UncaughtExceptionHandler是一个接口,在Thread中。里面只有一个方法uncaughtException。当我们注册一个UncaughtExceptionHandler之后,当我们的程序crash时就会回调uncaughtException方法,而uncaughtException方法带有两个参数,参数中就存放这crash信息。接下来只看写代码


package hi.xiaoyu.crashhandler;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.Thread.UncaughtExceptionHandler;
import java.util.Date;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

public class CrashHandler implements UncaughtExceptionHandler {

  private static CrashHandler instance;

  public static CrashHandler getInstance() {
    if (instance == null) {
      instance = new CrashHandler();
    }
    return instance;
  }

  public void init(Context ctx) {
    Thread.setDefaultUncaughtExceptionHandler(this);
  }

  /**
   * 核心方法,当程序crash 会回调此方法, Throwable中存放这错误日志
   */
  @Override
  public void uncaughtException(Thread arg0, Throwable arg1) {

    String logPath;
    if (Environment.getExternalStorageState().equals(
        Environment.MEDIA_MOUNTED)) {
      logPath = Environment.getExternalStorageDirectory()
          .getAbsolutePath()
          + File.separator
          + File.separator
          + "log";

      File file = new File(logPath);
      if (!file.exists()) {
        file.mkdirs();
      }
      try {
        FileWriter fw = new FileWriter(logPath + File.separator
            + "errorlog.log", true);
        fw.write(new Date() + "\n");
        // 错误信息
        // 这里还可以加上当前的系统版本,机型型号 等等信息
        StackTraceElement[] stackTrace = arg1.getStackTrace();
        fw.write(arg1.getMessage() + "\n");
        for (int i = 0; i < stackTrace.length; i++) {
          fw.write("file:" + stackTrace[i].getFileName() + " class:"
              + stackTrace[i].getClassName() + " method:"
              + stackTrace[i].getMethodName() + " line:"
              + stackTrace[i].getLineNumber() + "\n");
        }
        fw.write("\n");
        fw.close();
        // 上传错误信息到服务器
        // uploadToServer();
      } catch (IOException e) {
        Log.e("crash handler", "load file failed...", e.getCause());
      }
    }
    arg1.printStackTrace();
    android.os.Process.killProcess(android.os.Process.myPid());
  }

}

在Activity或者Application中注册一下即可


CrashHandler crashHandler = CrashHandler.getInstance();
crashHandler.init(getApplicationContext());

这样就实现了Android全局异常的捕获处理,实现过程也比较简单,希望对大家学习Android软件编程有所帮助。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务