首页 > 代码库 > 全局监听Crash

全局监听Crash

package com.example.crashcatch;import java.io.File;import java.io.IOException;import java.lang.Thread.UncaughtExceptionHandler;import java.lang.reflect.Field;import java.text.SimpleDateFormat;import java.util.Date;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Set;import android.content.Context;import android.content.Intent;import android.os.Build;import android.os.Environment;import android.util.Log;import android.widget.Toast;/** * 处理系统未捕捉到的异常,防止app崩溃出现的不好体验 * @author yWX206812 * */public class CrashHandler implements Thread.UncaughtExceptionHandler{	CrashApplication crashApplication;	UncaughtExceptionHandler defaultUncaughtExceptionHandler;	SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-mm-dd_HH-mm-ss");	private static CrashHandler crashHandler=new CrashHandler();			private CrashHandler() {		super();		// TODO Auto-generated constructor stub	}		public static CrashHandler getInstance(){		return crashHandler;	}	public void init(CrashApplication crashApplication){		this.crashApplication=crashApplication;		defaultUncaughtExceptionHandler=Thread.getDefaultUncaughtExceptionHandler();		Thread.setDefaultUncaughtExceptionHandler(this);	}		@Override	public void uncaughtException(Thread thread, Throwable ex) {		// TODO Auto-generated method stub		if(!catchHandler(ex)){			defaultUncaughtExceptionHandler.uncaughtException(thread, ex);		}	}			/**	 * 异常处理	 * @param ex	 * @return	 */	public boolean catchHandler(Throwable ex){		boolean result=false;		try {			Log.e("------------------异常捕获--------------","写入文档");			result= saveExceptionInfo(ex);			Toast.makeText(crashApplication.getCurrentContext(), "程序异常", Toast.LENGTH_SHORT).show();			crashApplication.getCurrentContext().sendBroadcast(new Intent(CrashActivity.crashAction));			Log.e("------------------异常捕获--------------","发送广播");		} catch (IllegalArgumentException e) {			// TODO Auto-generated catch block			e.printStackTrace();		} catch (IllegalAccessException e) {			// TODO Auto-generated catch block			e.printStackTrace();		} catch (IOException e) {			// TODO Auto-generated catch block			e.printStackTrace();		}		return result;	}		/**	 * 获取设备和app的基本信息	 * @return	 * @throws IllegalAccessException 	 * @throws IllegalArgumentException 	 */	public Map<String, String> getBaseInfo() throws IllegalArgumentException, IllegalAccessException{		Map<String, String> map=new HashMap<String, String>();		long currTime=System.currentTimeMillis();		String timeStr=simpleDateFormat.format(new Date(currTime));		map.put("当前时间", timeStr);		Class bClass=Build.class.getClass();		Field[] fields=bClass.getFields();		for (int i = 0; i < fields.length; i++) {			String key=fields[i].getName();			String val=fields[i].get(null).toString();			map.put(key, val);		}		return map;	}		public boolean saveExceptionInfo(Throwable ex) throws IllegalArgumentException, IllegalAccessException, IOException{		Map<String, String> map=getBaseInfo();		map.put("", ex.getMessage());		Set<String> keys=map.keySet();		Iterator<String> iterator=keys.iterator();		StringBuilder sb=new StringBuilder();		while (iterator.hasNext()) {			String key=iterator.next();			String val=map.get(key);			sb.append(key+":"+val+"\n");		}		String packageName=crashApplication.getCurrentContext().getPackageName();		if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){			File sdDir=Environment.getExternalStorageDirectory();			File dir=new File(sdDir,packageName+"/"+"crash_log");			if(!dir.exists()){				dir.mkdirs();			}			String currLogTime=simpleDateFormat.format(new Date(System.currentTimeMillis()));			String fileName="crash_"+currLogTime+".log";			return SDCardUtil.writeFile(dir, fileName,sb.toString());		}		return false;	}	}
package com.example.crashcatch;import android.app.Activity;import android.app.AlertDialog;import android.content.BroadcastReceiver;import android.content.Context;import android.content.DialogInterface;import android.content.Intent;import android.content.IntentFilter;import android.os.Bundle;import android.util.Log;public class CrashActivity extends Activity {	public static final String crashAction="CrashCatch";	public CrashReceiver crashReceiver=new CrashReceiver();	@Override	protected void onCreate(Bundle savedInstanceState) {		// TODO Auto-generated method stub		super.onCreate(savedInstanceState);		setContentView(R.layout.activity_main);		((CrashApplication)getApplication()).setCurrentContext(this);		Log.e("-----------------crashReceiver-----------------","注册广播");		IntentFilter intentFilter=new IntentFilter();		intentFilter.addAction(CrashActivity.crashAction);		registerReceiver(crashReceiver, intentFilter);		Log.e("-----------------crashReceiver-----------------","注册完毕");	}		class CrashReceiver extends BroadcastReceiver{		@Override		public void onReceive(Context context, Intent intent) {			// TODO Auto-generated method stub			Log.e("-----------------crashReceiver-----------------",intent.getAction());			if(intent.getAction().equals(crashAction)){				new AlertDialog.Builder(CrashActivity.this).setMessage("程序出现异常,是否退出").setNegativeButton("确认", new DialogInterface.OnClickListener() {										@Override					public void onClick(DialogInterface dialog, int which) {						// TODO Auto-generated method stub						unregisterReceiver(crashReceiver);						android.os.Process.killProcess(android.os.Process.myPid());						System.exit(0);					}				}).create().show();			}		}	}}

  

 

 

 SD卡检测

package com.example.crashcatch;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileWriter;import java.io.IOException;import android.util.Log;public class SDCardUtil {			/**	 * 读取文件返回字节数组	 * @param filePath	 * @return	 */	public static  byte[] readFile(String filePath) {		return new byte[1];	}	/**	 * 写入文件	 * @param filePath	 * @param bytes	 * @return	 * @throws IOException	 */	public static boolean writeFile(String filePath,String content) throws IOException {		File f=getFile(filePath);		if(f!=null){			f.delete();		}		f=new File(filePath);		try {			FileWriter fw=new FileWriter(f);			fw.write(content);			fw.close();			Log.e("------------------写入文件--------------","写入完毕");			return true;		} catch (FileNotFoundException e) {			// TODO Auto-generated catch block			throw e;		} catch (IOException e) {			// TODO Auto-generated catch block			throw e;		}	}		/**	 * 写入文件	 * @param dir	 * @param fileName	 * @param bytes	 * @return	 * @throws IOException	 */	public static boolean writeFile(File dir,String fileName,String content) throws IOException {		File f=getFile(dir,fileName);		if(f!=null){			f.delete();		}		f=new File(dir,fileName);		return writeFile(f.getAbsolutePath(), content);	}			public static File getFile(String filePath) {		File file = new File(filePath);		return getFile(file);	}	public static File getFile(File file) {		if (file.exists() && file.isFile()) {			return file;		}		return null;	}	public static File getFile(File dir, String fileName) {		File file = new File(dir, fileName);		return getFile(file);	}}