我是靠谱客的博主 热情小懒虫,最近开发中收集的这篇文章主要介绍Android 捕获全局异常,防止异常闪退,记录异常日志,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
import android.os.Looper;
import android.widget.Toast;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.Thread.UncaughtExceptionHandler;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * UncaughtException handler class
 * 
 */
public class CrashHandler implements UncaughtExceptionHandler {

	public static final String TAG = "CrashHandler";

	public static final String PROGRAM_BROKEN_ACTION = "com.teligen.wccp.PROGRAM_BROKEN";

	private UncaughtExceptionHandler mDefaultHandler;
	private static CrashHandler instance = new CrashHandler();
	private Context mContext;
	private Class<?> mainActivityClass;
	private Map<String, String> infos = new HashMap<String, String>();


	private CrashHandler() {
	}

	public static CrashHandler getInstance() {
		return instance;
	}

	public void init(Context context, Class<?> activityClass) {
		mContext = context;
		this.setMainActivityClass(activityClass);
		mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
		Thread.setDefaultUncaughtExceptionHandler(this);
	}


	@Override
	public void uncaughtException(Thread thread, Throwable ex) {
		if (!handleException(ex) && mDefaultHandler != null) {
			mDefaultHandler.uncaughtException(thread, ex);
		} else {
			System.out.println("uncaughtException--->" + ex.getMessage());
//			Log.e(TAG, ex.getMessage());
			logError(ex);
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
//				Log.e("debug", "error:", e);
			}
			exitApp();
		}
	}

	private boolean handleException(Throwable ex) {
		if (ex == null) {
			return false;
		}
		new Thread(new Runnable() {
			@Override
			public void run() {
				Looper.prepare();
				Toast.makeText(mContext.getApplicationContext(),
						"unknown exception and exiting...Please checking logs in sd card!", Toast.LENGTH_LONG).show();
				Looper.loop();
			}
		}).start();
		collectDeviceInfo(mContext.getApplicationContext());
		logError(ex);
		return true;
	}

	private void exitApp() {
		android.os.Process.killProcess(android.os.Process.myPid());
		System.exit(0);
	}

	public void collectDeviceInfo(Context ctx) {
		try {
			PackageManager pm = ctx.getPackageManager();
			PackageInfo pi = pm.getPackageInfo(ctx.getPackageName(),
					PackageManager.GET_ACTIVITIES);
			if (pi != null) {
				String versionName = pi.versionName == null ? "null"
						: pi.versionName;
				String versionCode = pi.versionCode + "";
				infos.put("versionName", versionName);
				infos.put("versionCode", versionCode);
			}
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		Field[] fields = Build.class.getDeclaredFields();
		for (Field field : fields) {
			try {
				field.setAccessible(true);
				infos.put(field.getName(), field.get(null).toString());
			} catch (Exception e) {
			}
		}
	}


	private void logError(Throwable ex) {

		StringBuffer sb = new StringBuffer();
		for (Map.Entry<String, String> entry : infos.entrySet()) {
			String key = entry.getKey();
			String value = entry.getValue();
			sb.append(key + "=" + value + "n");
		}
		int num = ex.getStackTrace().length;
		for (int i=0;i<num;i++){
			sb.append(ex.getStackTrace()[i].toString());
			sb.append("n");
		}

		File file = new File(filePath+"/log.txt");
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(file);
			fos.write((sb.toString()+"exception:"+ex.getLocalizedMessage()).getBytes());
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public Class<?> getMainActivityClass() {
		return mainActivityClass;
	}

	public void setMainActivityClass(Class<?> mainActivityClass) {
		this.mainActivityClass = mainActivityClass;
	}
}

filePath是记录日志的路径

在Applicaton中初始化

@Override
public void onCreate() {
    super.onCreate();
    CrashHandler mCrashHandler = CrashHandler.getInstance();
    mCrashHandler.init(getApplicationContext(), getClass());
    initFile();
}

 

 

 

最后

以上就是热情小懒虫为你收集整理的Android 捕获全局异常,防止异常闪退,记录异常日志的全部内容,希望文章能够帮你解决Android 捕获全局异常,防止异常闪退,记录异常日志所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(43)

评论列表共有 0 条评论

立即
投稿
返回
顶部