这篇文章是 ”友聊“ 开源项目学习系列的第五篇,前四篇文章请查看这里:
友聊 学习记录(一)
友聊 学习记录(二)
友聊 学习记录(三)
友聊 学习记录(四)
主要记录下友聊项目中 Utils 工具类的使用和处理。
首先看下项目中使用到哪些工具类
- DialogUtil
- LogUtil
- ToastUtil
- NetUtil
- SharePreferenceUtil
- TimeUtil 另外, 作者将HomeWatcher 和 SendMsgAsyncTask 放在了Util 包下面,尤其是SendMsgAsyncTask 不是特别恰当。当然,如果喜欢,anywhere is OK !
Github 上有开源的CommonUtil 工具类,包括的更多,更全, Github 地址 , 还有 作者博客地址, 文档说明在这里
真正称心如意,用着顺手的工具类,还是要自己去整理,编写。只有自己才知道,你是好哪口啊~
DialogUtil 类的封装:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
public static Dialog getLoginDialog(Activity context) {
final Dialog dialog = new Dialog(context, R.style.Dialog);
dialog.setContentView(R.layout.firset_dialog_view);
Window window = dialog.getWindow();
WindowManager.LayoutParams lp = window.getAttributes();
int screenW = getScreenWidth(context);
lp.width = (int) (0.6 * screenW);
TextView titleTxtv = (TextView) dialog.findViewById(R.id.tvLoad);
titleTxtv.setText(R.string.first_start_dialog_text);
return dialog;
}
public static Dialog getCustomDialog(Activity context) {
final Dialog dialog = new Dialog(context, R.style.Dialog);
return dialog;
}
/**
* 非activity的context获取自定义对话框
* @param context
* @return
*/
public static Dialog getWinDialog(Context context) {
final Dialog dialog = new Dialog(context, R.style.Dialog);
dialog.getWindow()
.setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
return dialog;
}
public static int getScreenWidth(Activity context) {
DisplayMetrics dm = new DisplayMetrics();
context.getWindowManager().getDefaultDisplay().getMetrics(dm);
return dm.widthPixels;
}
public static int getScreenHeight(Activity context) {
DisplayMetrics dm = new DisplayMetrics();
context.getWindowManager().getDefaultDisplay().getMetrics(dm);
return dm.heightPixels;
}
|
将Dialog 按照不同类型分类,加载,登陆,自定义,告警,返回不同的dialog 对象。有没有更好的封装方法呢?
将dialog 类型作为参数传进来。
HomeWatcher 封装,homewatcher 监听home 键事件,点击,长按。看下代码部分
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
// 回调接口
public interface OnHomePressedListener {
public void onHomePressed();
public void onHomeLongPressed();
}
// 定义广播
class InnerRecevier extends BroadcastReceiver {
final String SYSTEM_DIALOG_REASON_KEY = "reason";
final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";
final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";
final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
if (reason != null) {
L.i(TAG, "action:" + action + ",reason:" + reason);
if (mListener != null) {
if (reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)) {
// 短按home键
mListener.onHomePressed();
} else if (reason
.equals(SYSTEM_DIALOG_REASON_RECENT_APPS)) {
// 长按home键
mListener.onHomeLongPressed();
}
}
}
}
}
}
|
SharePreferenceUtil 封装: 一个构造函数,其余的都是get 和 set 方法。
NetUtil : 网络工具类,封装的比较简单,简单来说,就一个判断网络连接状态的方法。这个工具类里面应该包含其他的方法,比如是否漫游,网络类型。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public static boolean isNetConnected(Context context) {
boolean isNetConnected;
// 获得网络连接服务
ConnectivityManager connManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = connManager.getActiveNetworkInfo();
if (info != null && info.isAvailable()) {
// String name = info.getTypeName();
// L.i("当前网络名称:" + name);
isNetConnected = true;
} else {
L.i("没有可用网络");
isNetConnected = false;
}
return isNetConnected;
}
|
TimeUtil : 封装的好,会减少很多代码。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
public static String getTime(long time) {
SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd HH:mm");
return format.format(new Date(time));
}
public static String getHourAndMin(long time) {
SimpleDateFormat format = new SimpleDateFormat("HH:mm");
return format.format(new Date(time));
}
public static String getChatTime(long timesamp) {
String result = "";
SimpleDateFormat sdf = new SimpleDateFormat("dd");
Date today = new Date(System.currentTimeMillis());
Date otherDay = new Date(timesamp);
int temp = Integer.parseInt(sdf.format(today))
- Integer.parseInt(sdf.format(otherDay));
switch (temp) {
case 0:
result = "今天 " + getHourAndMin(timesamp);
break;
case 1:
result = "昨天 " + getHourAndMin(timesamp);
break;
case 2:
result = "前天 " + getHourAndMin(timesamp);
break;
default:
// result = temp + "天前 ";
result = getTime(timesamp);
break;
}
return result;
}
|
LogUtil 和 ToastUtil 的封装就不再贴出,简单的设置下开关,方法。
今天是元宵节和情人节喔,十九年遇一次,祝大家节日快乐~