1
+ package com.liubuyao.utils
2
+
3
+ import android.app.Application
4
+ import android.content.Context
5
+ import android.os.Build
6
+
7
+ /* *
8
+ * 用于初始化context全局调用使用
9
+ *
10
+ * @author lby
11
+ */
12
+ object AppUtils {
13
+
14
+ @Volatile
15
+ var application: Application ? = null
16
+
17
+ fun init (context : Context ? ) {
18
+ if (context == null ) {
19
+ throw NullPointerException (" 未初始化Library" )
20
+ }
21
+ application = context.applicationContext as Application ?
22
+ }
23
+
24
+ fun getApp (): Application {
25
+ if (application == null ) {
26
+ throw NullPointerException (" 未初始化Library" )
27
+ }
28
+ return application!!
29
+ }
30
+
31
+ fun getPackageName (): String {
32
+ return getApp().packageName
33
+ }
34
+
35
+ fun getVersionName (): String {
36
+ return getVersionName(getPackageName())
37
+ }
38
+
39
+ fun getVersionName (packageName : String? ): String {
40
+ if (packageName.isNullOrEmpty()) {
41
+ return " "
42
+ }
43
+ val pm = getApp().packageManager
44
+ val packageInfo = pm.getPackageInfo(packageName, 0 )
45
+ return if (packageInfo == null ) " " else packageInfo.packageName
46
+ }
47
+
48
+ fun getVersionCode (): Int {
49
+ return getVersionCode(getPackageName())
50
+ }
51
+
52
+ fun getVersionCode (packageName : String? ): Int {
53
+ if (packageName.isNullOrEmpty()) {
54
+ return - 1
55
+ }
56
+ val pm = getApp().packageManager
57
+ val packageInfo = pm.getPackageInfo(packageName, 0 )
58
+ return packageInfo?.versionCode ? : - 1
59
+ }
60
+
61
+ fun getSDKVersionName (): String {
62
+ return Build .VERSION .RELEASE
63
+ }
64
+
65
+ fun getSDKVersionCode (): Int {
66
+ return Build .VERSION .SDK_INT
67
+ }
68
+
69
+ /* *
70
+ * 制造商
71
+ */
72
+ fun getManufacturer (): String {
73
+ return Build .MANUFACTURER
74
+ }
75
+
76
+ /* *
77
+ * 手机厂商
78
+ */
79
+ fun getBrand (): String {
80
+ return Build .BRAND
81
+ }
82
+
83
+ /* *
84
+ * 型号
85
+ */
86
+ fun getModel (): String {
87
+ return Build .MODEL
88
+ }
89
+
90
+ /* *
91
+ * 获取ABI
92
+ */
93
+ fun getABI (): String {
94
+ return Build .CPU_ABI
95
+ }
96
+
97
+ }
0 commit comments