系统未知异常捕获和错误日志保存及上传 (崩溃异常)_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 系统未知异常捕获和错误日志保存及上传 (崩溃异常)

系统未知异常捕获和错误日志保存及上传 (崩溃异常)

 2014/9/17 12:26:13  zheyiw  程序员俱乐部  我要评论(0)
  • 摘要://1,自定义Application捕获未知异常publicclassHKBaseApplicationextendsApplication{//activity对象列表,用于activity统一管理privateList<Activity>activityList;//异常捕获protectedbooleanisNeedCaughtExeption=true;//是否捕获未知异常privatePendingIntentrestartIntent
  • 标签:上传 未知 异常捕获 错误 异常
class="java" name="code">

//1,自定义Application捕获未知异常
public class HKBaseApplication extends Application {
	// activity对象列表,用于activity统一管理
	private List<Activity> activityList;
	// 异常捕获
	protected boolean isNeedCaughtExeption = true;// 是否捕获未知异常
	private PendingIntent restartIntent;
	private MyUncaughtExceptionHandler uncaughtExceptionHandler;
	public String packgeName;
	public String crashFilePath;

	@Override
	public void onCreate() {
		super.onCreate();

		activityList = new ArrayList<Activity>();
		packgeName = getPackageName();
		crashFilePath = Environment.getExternalStorageDirectory() + "/HKDownload/" + packgeName + "/crash/";

		if (isNeedCaughtExeption) {
			cauchException();
		}
	}

	// -------------------异常捕获-----捕获异常后重启系统-----------------//

	private void cauchException() {
		Intent intent = new Intent();
		// 参数1:包名,参数2:程序入口的activity
		intent.setClassName(packgeName, packgeName + ".LoginActivity");
		restartIntent = PendingIntent.getActivity(getApplicationContext(), -1, intent,
				Intent.FLAG_ACTIVITY_NEW_TASK);

		// 程序崩溃时触发线程
		uncaughtExceptionHandler = new MyUncaughtExceptionHandler();
		Thread.setDefaultUncaughtExceptionHandler(uncaughtExceptionHandler);
	}

	// 创建服务用于捕获崩溃异常
	private class MyUncaughtExceptionHandler implements UncaughtExceptionHandler {
		@Override
		public void uncaughtException(Thread thread, Throwable ex) {
			// 保存错误日志
			saveCatchInfo2File(ex);

			// 1秒钟后重启应用
			AlarmManager mgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
			mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, restartIntent);

			// 关闭当前应用
			finishAllActivity();
			finishProgram();
		}
	};

	/**
	 * 保存错误信息到文件中
	 * 
	 * @return 返回文件名称
	 */
	private String saveCatchInfo2File(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();
		}
		printWriter.close();
		String sb = writer.toString();
		try {
			DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
			String time = formatter.format(new Date());
			String fileName = time + ".txt";
			System.out.println("fileName:" + fileName);
			if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
				File dir = new File(crashFilePath);
				if (!dir.exists()) {
					if (!dir.mkdirs()) {
						// 创建目录失败: 一般是因为SD卡被拔出了
						return "";
					}
				}
				System.out.println("filePath + fileName:" + crashFilePath + fileName);
				FileOutputStream fos = new FileOutputStream(crashFilePath + fileName);
				fos.write(sb.getBytes());
				fos.close();
				// 文件保存完了之后,在应用下次启动的时候去检查错误日志,发现新的错误日志,就发送给开发者
			}
			return fileName;
		} catch (Exception e) {
			System.out.println("an error occured while writing file..." + e.getMessage());
		}
		return null;
	}

	// ------------------------------activity管理-----------------------//

	// activity管理:从列表中移除activity
	public void removeActivity(Activity activity) {
		activityList.remove(activity);
	}

	// activity管理:添加activity到列表
	public void addActivity(Activity activity) {
		activityList.add(activity);
	}

	// activity管理:结束所有activity
	public void finishAllActivity() {
		for (Activity activity : activityList) {
			if (null != activity) {
				activity.finish();
			}
		}
	}

	// 结束线程,一般与finishAllActivity()一起使用
	// 例如: finishAllActivity;finishProgram();
	public void finishProgram() {
		android.os.Process.killProcess(android.os.Process.myPid());
	}
}


//2,在loginActivity中添加下面的代码发送异常日志给开发者
@Override
protected void onResume() {
	super.onResume();
	String newCrashFileName = getNewCrashFile(app.crashFilePath);
	System.out.println("newCrashFileName:" + newCrashFileName);
	UtilPre.save(this, UtilPre.Str.newCrashFileName, newCrashFileName);
	TaskGetWhenNotLoginedIn task = new TaskGetWhenNotLoginedIn(this, "", new String[] {}) {
		public void onTaskSuccess(DataTable[] ds, boolean isAsk, String msg, ArrayList<String> msgList) {
			// 忽略返回和错误
		}

		@Override
		public void onTaskFailed(JSONObject result, String message) {
			// 忽略返回和错误
		}
	};
	task.executeInBackground();
}

public static String getNewCrashFile(String filePath) {
	File root = new File(filePath);
	File[] files = root.listFiles();
	String maxFileName = "";
	for (File file : files) {
		String fileName = file.getName();
		System.out.println("遍历错误日志:" + fileName);
		if (maxFileName.compareTo(fileName) < 0) {
			maxFileName = fileName;
		}
	}
	return maxFileName;
}

发表评论
用户名: 匿名