1--在manifest添加:<application android:icon="@drawable/icon" android:label="@string/login_title" android:theme="@style/skin" android:name="com.XXX.application.LauncherApplication" android:process="@string/process" >public class LauncherApplication extends Application {private Context context;@Overridepublic void onCreate () {super.onCreate();AppExcepiton appException = AppExcepiton.getInstance(); appException.init(getApplicationContext()); }2—public class AppExcepiton implements UncaughtExceptionHandler {// 获取application 对象;private Context mContext;private Thread.UncaughtExceptionHandler defaultExceptionHandler;// 单例声明CustomException;private static AppExcepiton appException;private AppExcepiton() {}public static AppExcepiton getInstance() {if (appException == null) {appException = new AppExcepiton();}return appException;}@Overridepublic void uncaughtException(Thread thread, Throwable exception) {// TODO Auto-generated method stubString path = null;if (defaultExceptionHandler != null) {String state = Environment.getExternalStorageState();// 判断SdCard是否存在并且是可用的if (Environment.MEDIA_MOUNTED.equals(state)) {path = Environment.getExternalStorageDirectory().getPath();}// 创建一个logcat目录path = path + "/eIVS/log";File file = new File(path);if (!file.exists()) {file.mkdir();}String time = getCurrentTime();String fileName = time.substring(0, 9);File myFile = new File(path+"/"+fileName+".log");String str = "\n"+time+"-->"+"["+exception.getLocalizedMessage()+"]";try {FileWriter fw = new FileWriter(myFile, true);fw.write(str);fw.flush();fw.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}Log.e("tag", "exception >>>>>>>" + exception.getLocalizedMessage());// 将异常抛出,则应用会弹出异常对话框.这里先注释掉// defaultExceptionHandler.uncaughtException(thread, exception);}}/** * 获得当前时间 * @return */public String getCurrentTime(){Time t = new Time();t.setToNow();int year = t.year;int month = t.month+1;int day = t.monthDay;int hour = t.hour;int minute = t.minute;int second = t.second;String time = year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second;return time;}public void init(Context context) {mContext = context;defaultExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();Thread.setDefaultUncaughtExceptionHandler(this);}
3--在一个activity中模拟异常:
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.login); // 初始化资源信息 throw new RuntimeException("--------");// init(); }
/** * 删除超过一年的日志 * @param path */ public void deleteOldFile(final String path){ File file = new File(path); file.list(new FilenameFilter() { @Override public boolean accept(File dir, String filename) { // TODO Auto-generated method stub File file = new File(path+"/"+filename); Long ago = file.lastModified(); Long now = System.currentTimeMillis(); //如果最后一次修改时间超过一年:3153600秒 if((now-ago) > 31536000){ file.delete(); } return false; } }); }
//打印 有用信息 能够指出 错误代码的行数
@Overridepublic void uncaughtException(Thread thread, Throwable exception) {// TODO Auto-generated method stubStackTraceElement[] stack = exception.getCause().getStackTrace(); String path = null;if (defaultExceptionHandler != null) {String state = Environment.getExternalStorageState();// 判断SdCard是否存在并且是可用的if (Environment.MEDIA_MOUNTED.equals(state)) {path = Environment.getExternalStorageDirectory().getPath();}// 创建一个logcat目录path = path + "/eIVS/log";File file = new File(path);if (!file.exists()) {file.mkdir();}//删除过期文件deleteOldFile(path);String time = getCurrentTime();String fileName = time.substring(0, 9);File myFile = new File(path+"/"+fileName+".log"); try { String str = "\n"+time+"-->"; FileOutputStream fos = new FileOutputStream(myFile,true); fos.write(str.getBytes()); for (int i = 0; i < stack.length; i++) { fos.write(stack[i].toString().getBytes()); } fos.flush(); fos.close(); } catch (Exception e) { } }