1、背景介绍
在做Android开发过程中,开发阶段,我们可以通过DDMS看到输出的日志信息,或者是异常报错,这个时候一般都是运行时一场,比如空指针,内存溢出等等问题,我们在开始做开发的时候就可以得到这些信息。但是当我们的应用发布之后呢,对于不同的一些机型,我们可能有不同的错误类型,这个时候,我们无法得知应用程序的报错信息,就需要在客户端收集起来,回传到服务器。
2、异常捕获
Android中提供了一个全局异常的捕获,要点是:
1)实现UncaughtExceptionHandler,在方法uncaughtException中处理没有捕获的异常。
2)继承Application ,在其中调用Thread方法setDefaultUncaughtExceptionHandler,来捕获异常
别的不多说,代码如下:
/**
* Created by ljtyzhr on 2015/4/16.
*/
public class MyApplication extends Application {
public void onCreate(){
super.onCreate();
GlobalException handler = GlobalException.getInstance();
Thread.setDefaultUncaughtExceptionHandler(handler);
}
}
/**
* 构造方法私有化,获取得到一个单例
* Created by ljtyzhr on 2015/4/16.
*/
public class GlobalException implements UncaughtExceptionHandler{
private final static GlobalException myCrashHandler = new GlobalException();
private GlobalException(){
}
public static synchronized GlobalException getInstance(){
return myCrashHandler;
}
public void uncaughtException(Thread arg0, Throwable arg1){
// 在这里捕获异常信息
}
}
3、日志信息
我们可以通过命令行获取日志信息,当日志信息文件,到达一定的大小之后,就可以回传到服务器,供开发人员做分析使用。代码如下:
import android.content.Context;
import android.os.Environment;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* Created by ljtyzhr on 2015/4/16.
*/
public class LogcatHelper {
private static LogcatHelper INSTANCE = null;
private static String PATH_LOGCAT;
private LogDumper mLogDumper = null;
private int mPId;
/**
*
* 初始化目录
*
* */
public void init(Context context) {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {// 优先保存到SD卡中
PATH_LOGCAT = Environment.getExternalStorageDirectory()
.getAbsolutePath() + File.separator + "LogcatInfos";
} else {// 如果SD卡不存在,就保存到本应用的目录下
PATH_LOGCAT = context.getFilesDir().getAbsolutePath()
+ File.separator + "LogcatInfos";
}
File file = new File(PATH_LOGCAT);
if (!file.exists()) {
file.mkdirs();
}
}
public static LogcatHelper getInstance(Context context) {
if (INSTANCE == null) {
INSTANCE = new LogcatHelper(context);
}
return INSTANCE;
}
private LogcatHelper(Context context) {
init(context);
mPId = android.os.Process.myPid();
}
public void start() {
if (mLogDumper == null)
mLogDumper = new LogDumper(String.valueOf(mPId), PATH_LOGCAT);
mLogDumper.start();
}
public void stop() {
if (mLogDumper != null) {
mLogDumper.stopLogs();
mLogDumper = null;
}
}
private class LogDumper extends Thread {
private Process logcatProc;
private BufferedReader mReader = null;
private boolean mRunning = true;
String cmds = null;
private String mPID;
private FileOutputStream out = null;
public LogDumper(String pid, String dir) {
mPID = pid;
try {
out = new FileOutputStream(new File(dir, "Log-"
+ MyDate.getFileName() + ".log"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/**
*
* 日志等级:*:v , *:d , *:w , *:e , *:f , *:s
*
* 显示当前mPID程序的 E和W等级的日志.
*
* */
// cmds = "logcat *:e *:w | grep \"(" + mPID + ")\"";
// cmds = "logcat | grep \"(" + mPID + ")\"";//打印所有日志信息
cmds = "logcat -s test";//打印标签过滤信息
// cmds = "logcat *:e *:i | grep \"(" + mPID + ")\"";
}
public void stopLogs() {
mRunning = false;
}
@Override
public void run() {
try {
logcatProc = Runtime.getRuntime().exec(cmds);
mReader = new BufferedReader(new InputStreamReader(
logcatProc.getInputStream()), 1024);
String line = null;
while (mRunning && (line = mReader.readLine()) != null) {
if (!mRunning) {
break;
}
if (line.length() == 0) {
continue;
}
if (out != null && line.contains(mPID)) {
out.write((MyDate.getDateEN() + " " + line + "\n")
.getBytes());
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (logcatProc != null) {
logcatProc.destroy();
logcatProc = null;
}
if (mReader != null) {
try {
mReader.close();
mReader = null;
} catch (IOException e) {
e.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
out = null;
}
}
}
}
}