Android項目管理之配置管理android
在Android開發中不免會遇到一系列的配置管理,例如版本配置、數據庫版本號配置、預置數據配置、網絡接口配置、加密信息配置、日誌配置、異常捕獲配置等。本文將對這些內容給出一個較爲合理的解決方案。sql
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="xxx.xxx.xxx"
- android:versionCode="xxx"
- android:versionName="xxx" >
- #the output versioin
- version:9
- #appName
- appName:xxx
- #log open or close
- log:true
- #carch exception ;true catch , false not carch
- exception:false
- #database version,when database is modified ,plus 1
- dbversion:9
- /**
- * 系統參數初始化
- *
- * @param appllicationContext
- */
- public static void initSystemProperties(Context appllicationContext) {
- InputStream is = appllicationContext.getResources().openRawResource(
- R.raw.system);
- Properties properties = new Properties();
- try {
- properties.load(is);
- Variable.versionCode = Integer.parseInt(properties.getProperty(
- Common.VERSION, "1"));
- Variable.isLogOpened = Boolean.parseBoolean(properties.getProperty(
- Common.LOG, "false"));
- Variable.dbVersion = Integer.parseInt(properties.getProperty(
- Common.DBVERSION, "1"));
- Variable.appName = properties.getProperty(Common.APPNAME);
- Variable.dbchange = properties.getProperty(Common.DBCHANGE, "");
- Variable.isExceptionOpened = Boolean.parseBoolean(properties.getProperty(
- Common.EXCEPTION, "false"));
- } catch (IOException e) {
- Util.error(e.getMessage());
- } finally {
- Util.closeStream(is);
- }
- }
- public static DBHelper getInstance() {
- if (helper == null) {
- helper = new DBHelper(Variable.appllicationContext, DB_NAME, null,
- Variable.dbVersion);
- }
- return helper;
- }
- /**
- * Common log
- *
- * @param tag
- * @param msg
- * @param level
- */
- public static void log(String tag, String msg, int level) {
- if (Variable.isLogOpened) {
- String msgs = msg;
- String tags = tag;
- Exception e = new Exception();
- StackTraceElement[] els = e.getStackTrace();
- String logDetails = Common.PACKAGENAME;
- for (int i = 0, l = els.length; i < l; i++) {
- if (els[i].getClassName().startsWith(Common.PACKAGENAME)
- && !els[i].getClassName().equals(Util.class.getName())) {
- logDetails = els[i].getFileName() + "->"
- + els[i].getMethodName() + ":"
- + els[i].getLineNumber() + " ";
- msgs = logDetails + msgs;
- if ("".equals(tags)) {
- tags = els[i].getFileName().substring(0,
- els[i].getFileName().indexOf("."));
- }
- break;
- }
- }
- switch (level) {
- case Log.DEBUG:
- Log.d(tags, msgs);
- break;
- case Log.INFO:
- Log.i(tags, msgs);
- break;
- case Log.WARN:
- Log.w(tags, msgs);
- break;
- case Log.ERROR:
- Log.e(tags, msgs);
- break;
- default:
- Log.d(tag, msgs);
- break;
- }
- }
- }
- /**
- * Simple log
- *
- * @param tag
- * @param msg
- */
- public static void log(String tag, String msg) {
- log(tag, msg, Log.WARN);
- }
- public static void log(String msg) {
- log("", msg);
- }
- public static void info(String msg) {
- log("", msg, Log.INFO);
- }
- public static void error(String msg) {
- log("", msg, Log.ERROR);
- }
- public static void debug(String msg) {
- log("", msg, Log.DEBUG);
- }
- if(Variable.isExceptionOpened){
- CrashHandler handler = CrashHandler.getInstance();
- handler.init(getApplicationContext());
- Thread.setDefaultUncaughtExceptionHandler(handler);
- }