`
zheyiw
  • 浏览: 998432 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

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

阅读更多

//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中添加下面的代码发送异常日志给开发者
// 发送错误日志
private void sendCrashLog() {
	String lastCrashFileName = UtilPre.getString(this, UtilPre.Str.newCrashFileName, "");
	final String newCrashFileName = getNewCrashFile(app.crashFilePath, lastCrashFileName);
	if (lastCrashFileName.compareTo(newCrashFileName) < 0) {
		System.out.println("newCrashFileName:" + newCrashFileName);
		String ErrorMsg = UtilFile.readFromFile(app.crashFilePath, lastCrashFileName, true);
		// IMEI, ServiceIP, ClientType, ClientVersion, ErrorMsg
		TaskGetWhenNotLoginedIn task = new TaskGetWhenNotLoginedIn(this, "SDI_ClientError", new String[] {
				imei, Config.URLServer, "FAI_Android", currentVersionNo, ErrorMsg }) {
			public void onTaskSuccess(DataTable[] ds, boolean isAsk, String msg, ArrayList<String> msgList) {
				UtilPre.save(activity, UtilPre.Str.newCrashFileName, newCrashFileName);
			}

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

// 获取新的错误日志文件名
private String getNewCrashFile(String filePath, String lastCrashFileName) {
	File root = new File(filePath);
	File[] files = root.listFiles();
	for (File file : files) {
		String fileName = file.getName();
		// System.out.println("遍历错误日志:" + fileName);
		if (lastCrashFileName.compareTo(fileName) < 0) {
			lastCrashFileName = fileName;
		}
	}
	return lastCrashFileName;
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics