全局异常捕获
废话不多说,直接上代码
package com.util;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.Thread.UncaughtExceptionHandler;
import android.content.Context;
/**
* 全局异常捕获句柄类
*/
public class CrashHandler implements UncaughtExceptionHandler {
public static final String TAG = "CrashHandler";
public static final boolean DEBUG = false;
private static CrashHandler crashHandler;
private Thread.UncaughtExceptionHandler exceptionHandler;
private Context context;
/**
* 私有构造函数
*/
private CrashHandler() {
}
/**
* @return 单例
*/
public static CrashHandler getInstance() {
if (crashHandler == null) {
crashHandler = new CrashHandler();
}
return crashHandler;
}
/**
* @param context
* 上下文对象
*/
public void init(Context context) {
this.context = context;
exceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(this);
}
/**
* 当异常发生时,捕获异常
*
* @param thread
* 线程
* @param ex
* 异常
*/
@Override
public void uncaughtException(Thread thread, Throwable ex) {
if (!handleException(thread, ex) && exceptionHandler != null) {
exceptionHandler.uncaughtException(thread, ex);
} else {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
SLog.Console("===============CrashHandler================");
SLog.saveException(e);
SLog.Console("==============CrashHandlerEnd==============");
}
}
}
/**
* 自定义异常处理
*
* @param ex
* 异常
* @return boolean 是否处理
*/
private boolean handleException(Thread thread, Throwable ex) {
if (ex == null) {
return true;
}
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw, true);
ex.printStackTrace(pw);
pw.flush();
sw.flush();
this.saveInfo2File(ex);
return true;
}
/**
* 保存异常信息到文件中
*
* @param ex
* 异常
* @return String 异常信息
*/
public String saveInfo2File(final Throwable ex) {
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();
}
String info = writer.toString();
printWriter.close();
FileUtil.saveLog("=================CrashInfo===============");
FileUtil.saveLog(info);
SLog.Console(info);
FileUtil.saveLog("================CrashInfoEnd=============");
return info;
}
}
然后在MainApplication中添加对应的声明即可
package com.example.zxingdemo;
import android.app.Application;
import com.util.CrashHandler;
import com.util.PathUtil;
import com.util.SLog;
public class MainApplication extends Application {
private static MainApplication mApplication;
@Override
public void onCreate() {
super.onCreate();
mApplication = this;
PathUtil.getInstance().init(mApplication);
SLog.cleanOutOfDateLog();
CrashHandler crashHandler = CrashHandler.getInstance();
crashHandler.init(getApplicationContext());
}
public static synchronized MainApplication getInstance() {
return mApplication;
}
}
最后一定要注意在Manifest里边指定Application
<application
android:name="com.example.zxingdemo.MainApplication"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.karics.library.zxing.android.CaptureActivity"
android:theme="@android:style/Theme.NoTitleBar"
android:screenOrientation="portrait" >
</activity>
</application>