安卓地址管理器AddressManager


弃用,有错误

工具类

public class AddressManager {

private static Properties properties;

public static Application APP;

private static AddressManager instance = null;

// host

private static String host = "";

public AddressManager(Context context) {

InputStream is = null;

try {

is = APP.getAssets().open("address.properties");

properties = new Properties();

properties.load(is);

} catch (IOException e) {

e.printStackTrace();

} finally {

Util.closeStream(is);

}

}

public static void setApplication(Application application){

APP=application;

}

/**

* 实例化<br>

* 该方法在系统启动的时候调用

*

* @param context

* @return

*/

public static AddressManager getInstance(Context context) {

APP = (Application)context;

if (instance == null) {

instance = new AddressManager(context);

}

return instance;

}

/**

* 取值<br>

* 输入的参数为raw/address.properties中的key

*

* @param key

* @param defaultValue

* @return

*/

public static String get(String key, String defaultValue) {

return properties.getProperty(key, defaultValue);

}

/**

* 取值<br>

* 输入的参数为raw/address.properties中的key

*

* @param key

* @return

*/

public static String get(String key) {

return properties.getProperty(key, "");

}

/**

* 获取完整的URL地址<br>

* 输入的参数为raw/address.properties中的key

*

* @param key

* @param defaultValue

* @return

*/

public static String getUrl(String key, String defaultValue) {

return getHost() + get(key, defaultValue);

}

/**

* 获取完整的URL地址<br>

* 输入的参数为raw/address.properties中的key

*

* @param key

* @return

*/

public static String getUrl(String key) {

return getHost() + properties.getProperty(key, "");

}

/**

* 获取主机<br>

* 获取主机地址

*

* @return

*/

public static String getHost() {

if (TextUtils.isEmpty(host)) {

host = properties.getProperty("host", "");

}

return host;

}

}

使用方法:

1.在首页activity中加入

AddressManager.setApplication(getApplication());

new AddressManager(this);

2.在moudle目录下创建新文件夹命名assets,新建文件address.properties

文件举例:

host:http://www.wuxi.gov.cn/