我是靠谱客的博主 会撒娇冰淇淋,这篇文章主要介绍Android代码中自己写的一些工具类(整理),现在分享给大家,希望可以做个参考。

这些都是在项目中会用到的一些代码;当做备忘

整理者:Insomnia

因为开发的时候,经常有些代码会重复使用,故将其记录下开。以免遗忘

看码:

1.Log日志管理类

复制代码
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
import android.util.Log; /** * Log 日志工具类 */ public class Logger { public static int LOG_LEVEL = 6; public static int ERROR = 1; public static int WARNING = 2; public static int INFO = 3; public static int DEBUG = 4; public static int VERBOSE = 5; public static void e(String tag, String message) { if (LOG_LEVEL > ERROR) { Log.e(tag, message); } } public static void w(String tag, String message) { if (LOG_LEVEL > WARNING) { Log.w(tag, message); } } public static void i(String tag, String message) { if (LOG_LEVEL > INFO) { Log.i(tag, message); } } public static void d(String tag, String message) { if (LOG_LEVEL > DEBUG) { Log.d(tag, message); } } public static void v(String tag, String message) { if (LOG_LEVEL > VERBOSE) { Log.v(tag, message); } } }

2.日期判断工具类

复制代码
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class DateUtil { /** * 对日期做出判断 * @param ordertime * @return */ public static int compare_date(String ordertime) { Date date = null; DateFormat df = null; try { df = new SimpleDateFormat("yyyy-MM-dd HH:mm"); date = df.parse(ordertime); String nowTime = df.format(new Date()); String orderNextTime = orderNextTime(Calendar.getInstance(), date); Date dt2 = df.parse(nowTime); Date dt3 = df.parse(orderNextTime); if (dt2.getTime() <= dt3.getTime()) { return 1; //正常 } else { return 0; // 超期 } } catch (ParseException e) { e.printStackTrace(); } return 0; } /** * 回退一天时间 * @param ca * @param date * @return */ private static String orderNextTime(Calendar ca, Date date) { ca.setTime(date); ca.add(Calendar.HOUR_OF_DAY, -2 * 12); String sb = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(ca.getTime()); return sb.toString(); } /** * 获得以前的时间 * @param ca * @param insertTime * @return */ public static String getOldTime(Calendar ca, String insertTime) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); Date date = null; try { date = sdf.parse(insertTime); } catch (ParseException e) { e.printStackTrace(); } ca.setTime(date); ca.add(Calendar.HOUR_OF_DAY, -12 * 3 * 2); String sb = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(ca.getTime()); return sb.toString(); } /** * 获取当前日期 * @return 当前日期(年-月-日 小时:分钟:秒) */ public static String getNowTime() { DateFormat df = null; String nowTime = null; try { df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); nowTime = df.format(new Date()); } catch (Exception e) { e.printStackTrace(); } return nowTime; } // 只获取年月日 public static String getYMD() { DateFormat df = null; String nowTime = null; try { df = new SimpleDateFormat("yyyy-MM-dd"); nowTime = df.format(new Date()); } catch (Exception e) { e.printStackTrace(); } return nowTime; } /** * 将String日期转换为Date格式的 * @param str 日期 * @return date格式的时间 */ public static Date getFormatTime(String str) { DateFormat df = null; Date date = null; try { df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); date = df.parse(str); } catch (Exception e) { e.printStackTrace(); } return date; } }

3.判断网络状态工具类

复制代码
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
import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.util.Log; public class NetCheck { /** * 判断是否有网络 * @param context * @return true:有网络;false:无网络 */ public static boolean isNetworkAvailable(Context context) { ConnectivityManager connectivity = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivity == null) { Log.i("NetWorkState", "Unavailabel"); return false; } else { NetworkInfo[] info = connectivity.getAllNetworkInfo(); if (info != null) { for (int i = 0; i < info.length; i++) { if (info[i].getState() == NetworkInfo.State.CONNECTED) { Log.i("NetWorkState", "Availabel"); return true; } } } } return false; } }

4.倒计时工具类

复制代码
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
package com.weijia.community.utils; import android.content.Context; import android.graphics.Color; import android.os.CountDownTimer; import android.widget.TextView; /*定义一个倒计时的内部类*/ public class MyCount extends CountDownTimer { private TextView button; private IsFinishListener isFinishListener; private Context context; private long millisUntilFinished1; private boolean isFromTask = false; public MyCount(long millisInFuture, long countDownInterval, TextView getCode, Context context, boolean isFromTask) { super(millisInFuture, countDownInterval); this.button = getCode; this.context = context; this.isFromTask = isFromTask; } @Override public void onFinish() { this.isFinishListener.FinishChange(); } @Override public void onTick(long millisUntilFinished) { millisUntilFinished1 = millisUntilFinished; button.setTextColor(Color.parseColor("#A4A4A4")); button.setClickable(false); if (this.isFromTask == false) { button.setText(millisUntilFinished / 1000 + "秒后重新获取"); } else { button.setText("剩余" + millisUntilFinished / 1000 + "秒"); } } // 实现该接口,使用全部被选择 public interface IsFinishListener { void FinishChange(); } public void setIsFinishListener(IsFinishListener isFinishListener) { this.isFinishListener = isFinishListener; } }

5.dp转px,px转dp

复制代码
1
2
3
4
5
6
7
8
9
10
//dp-->px public int dp2px(Context context, float dp) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dp * scale + 0.5f); } //px-->dp public int px2dp(Context context, float px) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (px / scale + 0.5f); }

最后

以上就是会撒娇冰淇淋最近收集整理的关于Android代码中自己写的一些工具类(整理)的全部内容,更多相关Android代码中自己写内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(97)

评论列表共有 0 条评论

立即
投稿
返回
顶部