android 仿微信框架,Android企业级架构-仿微信-功能组件建设

在前面的文章中,我们做了基础的网络库Lib、数据库Lib、UI库Lib,现在还有一节重要的Lib库等着我们去搭建,那就是功能库。

功能库中存放一些常用的工具类,比如时间/日期工具、文件处理工具、集合判断工具、Toast工具、SharedPreferences(偏好)工具等。下面我们开始处理

创建module:

64a6fa530733

创建Library

在工程目录下的build.gradle中ext中添加如下版本号:

// Common Library版本号

commonVersionCode = 1

commonVersionName = "1.0"

在common目录下的build.gradle中修改为如下样式(保持与其它Library相同样式):

apply plugin: 'com.android.library'

android {

compileSdkVersion rootProject.ext.compileSdkVersion

buildToolsVersion rootProject.ext.buildToolsVersion

defaultConfig {

minSdkVersion rootProject.ext.minSdkVersion

targetSdkVersion rootProject.ext.targetSdkVersion

versionCode rootProject.ext.commonVersionCode

versionName rootProject.ext.commonVersionName

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

}

buildTypes {

release {

minifyEnabled false

proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

}

}

}

dependencies {

compile fileTree(dir: 'libs', include: ['*.jar'])

androidTestCompile("com.android.support.test.espresso:espresso-core:$rootProject.ext.androidTestCompileVersion", {

exclude group: 'com.android.support', module: 'support-annotations'

})

compile "com.android.support:appcompat-v7:$rootProject.ext.supportVersion"

testCompile "junit:junit:$rootProject.ext.junitVersion"

}

在common目录下,打开src->main下的AndroidManifest.xml,将application标签删除。

接下来,我们先在common目录下的com.monch.common包下,添加第一个工具类,在Android中最常用的就是Toast,笔者的习惯是创建一个类,起名叫T.java,代码如下:

public final class T {

private T() {}

public static void ss(Context context, @StringRes int id) {

Toast.makeText(context, id, Toast.LENGTH_SHORT).show();

}

public static void ss(Context context, CharSequence text) {

Toast.makeText(context, text, Toast.LENGTH_SHORT).show();

}

public static void sl(Context context, @StringRes int id) {

Toast.makeText(context, id, Toast.LENGTH_LONG).show();

}

public static void sl(Context context, CharSequence text) {

Toast.makeText(context, text, Toast.LENGTH_LONG).show();

}

}

这个类我们简单处理一下,如果有需要修改Toast的样式或其它需求,可以直接在这里修改。这样大家在使用的时候,会非常方便,例如T.ss("xxx");或T.sl("xxx"); 这两个方法的区别是显示的时间长短。

再来个字符串工具类

public class StringUtils {

private StringUtils() {}

public static boolean isEmpty(CharSequence cs) {

return cs == null || cs.length() == 0;

}

public static boolean isNotEmpty(CharSequence cs) {

return !isEmpty(cs);

}

public static boolean equals(String str1, String str2) {

return (str1 == null && str2 == null) ||

(isNotEmpty(str1) && str1.equals(str2));

}

public static boolean isWebSite(String string) {

return isNotEmpty(string) && string.matches(Patterns.WEB_URL.pattern());

}

public static boolean isPhone(String string) {

return isNotEmpty(string) && string.matches(Patterns.PHONE.pattern());

}

public static boolean isEmail(String string) {

return isNotEmpty(string) && string.matches(Patterns.EMAIL_ADDRESS.pattern());

}

}

再来一个偏好存储工具类

public final class PreferenceUtils {

private PreferenceUtils() {}

private static SharedPreferences sp;

/**

* 获取偏好执行器

* @return

*/

public static void init(Context context) {

sp = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);

}

/**

* 添加偏好内容:异步,不会引起长时间等待

* @param key

* @param value

*/

public static void put(String key, Object value) {

if (StringUtils.isEmpty(key) || value == null) return;

SharedPreferences.Editor editor = sp.edit();

if (value instanceof String ||

value instanceof StringBuilder ||

value instanceof StringBuffer) {

editor.putString(key, String.valueOf(value));

editor.apply();

} else if (value instanceof Integer) {

editor.putInt(key, (int) value);

editor.apply();

} else if (value instanceof Long) {

editor.putLong(key, (long) value);

editor.apply();

} else if (value instanceof Float) {

editor.putFloat(key, (float) value);

editor.apply();

} else if (value instanceof Boolean) {

editor.putBoolean(key, (boolean) value);

editor.apply();

}

}

/**

* 添加偏好内容:同步,可能会引起长时间等待

* @param key

* @param value

* @return 添加是否成功

*/

public static boolean putSync(String key, Object value) {

if (StringUtils.isEmpty(key) || value == null)

return false;

SharedPreferences.Editor editor = sp.edit();

if (value instanceof String ||

value instanceof StringBuilder ||

value instanceof StringBuffer) {

editor.putString(key, String.valueOf(value));

return editor.commit();

} else if (value instanceof Integer) {

editor.putInt(key, (int) value);

return editor.commit();

} else if (value instanceof Long) {

editor.putLong(key, (long) value);

return editor.commit();

} else if (value instanceof Float) {

editor.putFloat(key, (float) value);

return editor.commit();

} else if (value instanceof Boolean) {

editor.putBoolean(key, (boolean) value);

return editor.commit();

}

return false;

}

/**

* 获取字符串

* @param key

* @return

*/

public static String getString(String key) {

return getString(key, null);

}

/**

* 获取字符串

* @param key

* @param defaultValue

* @return

*/

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

return sp.getString(key, defaultValue);

}

/**

* 获取整型

* @param key

* @return

*/

public static int getInt(String key) {

return getInt(key, 0);

}

/**

* 获取整型

* @param key

* @param defaultValue

* @return

*/

public static int getInt(String key, int defaultValue) {

return sp.getInt(key, defaultValue);

}

/**

* 获取长整型

* @param key

* @return

*/

public static long getLong(String key) {

return getLong(key, 0L);

}

/**

* 获取长整型

* @param key

* @param defaultValue

* @return

*/

public static long getLong(String key, long defaultValue) {

return sp.getLong(key, defaultValue);

}

/**

* 获取浮点型

* @param key

* @return

*/

public static float getFloat(String key) {

return getFloat(key, 0F);

}

/**

* 获取浮点型

* @param key

* @param defaultValue

* @return

*/

public static float getFloat(String key, float defaultValue) {

return sp.getFloat(key, defaultValue);

}

/**

* 获取布尔型

* @param key

* @return

*/

public static boolean getBoolean(String key) {

return getBoolean(key, false);

}

/**

* 获取布尔型

* @param key

* @param defaultValue

* @return

*/

public static boolean getBoolean(String key, boolean defaultValue) {

return sp.getBoolean(key, defaultValue);

}

/**

* 移除偏好内容:异步,不会引起长时间等待

* @param key

*/

public static void remove(String key) {

if (StringUtils.isEmpty(key)) return;

SharedPreferences.Editor editor = sp.edit();

editor.remove(key);

editor.apply();

}

/**

* 移除偏好内容:同步,可能会引起长时间等待

* @param key

* @return

*/

public static boolean removeSync(String key) {

if (StringUtils.isEmpty(key)) return false;

SharedPreferences.Editor editor = sp.edit();

editor.remove(key);

return editor.commit();

}

}

我们暂时先添加这几个工个类,接下来使用到什么,我们再添加就可以了。

查看源码请移步GitHub

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值