Преглед изворни кода

1.添加系统信息工具类

master
wangwanlei пре 10 месеци
родитељ
комит
b69d73ce04

+ 27
- 0
corelib/src/main/java/com/xhly/corelib/utils/CustomOSUtils.kt Прегледај датотеку

@@ -1,6 +1,11 @@
1 1
 package com.xhly.corelib.utils
2 2
 
3
+import android.bluetooth.BluetoothManager
4
+import android.content.Context
3 5
 import android.os.Build
6
+import androidx.core.content.ContextCompat.getSystemService
7
+import java.lang.reflect.Field
8
+import java.lang.reflect.Method
4 9
 
5 10
 class CustomOSUtils {
6 11
     companion object {
@@ -38,5 +43,27 @@ class CustomOSUtils {
38 43
         public fun getSystemDevice(): String {
39 44
             return Build.DEVICE
40 45
         }
46
+
47
+        /**
48
+         * 获取本机蓝牙地址, 确保蓝牙已开启,关闭状态无法获取到
49
+         */
50
+        public fun getBluetoothAddress(context: Context): String? {
51
+            try {
52
+                val bluetoothAdapter = (context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager).adapter
53
+                val field: Field = bluetoothAdapter.javaClass.getDeclaredField("mService")
54
+                field.isAccessible = true
55
+                val bluetoothManagerService: Any = field.get(bluetoothAdapter) ?: return null
56
+                val method: Method = bluetoothManagerService.javaClass.getMethod("getAddress")
57
+                val address: Any? = method.invoke(bluetoothManagerService)
58
+                return if (address != null && address is String) {
59
+                    address
60
+                } else {
61
+                    null
62
+                }
63
+            } catch (e: Exception) {
64
+                e.printStackTrace()
65
+            }
66
+            return null
67
+        }
41 68
     }
42 69
 }

+ 143
- 0
corelib/src/main/java/com/xhly/corelib/utils/FileSizeUtil.java Прегледај датотеку

@@ -0,0 +1,143 @@
1
+package com.xhly.corelib.utils;
2
+
3
+import android.app.ActivityManager;
4
+import android.content.Context;
5
+import android.os.Environment;
6
+import android.os.StatFs;
7
+
8
+import java.io.BufferedReader;
9
+import java.io.File;
10
+import java.io.FileReader;
11
+import java.io.IOException;
12
+import java.text.DecimalFormat;
13
+
14
+public class FileSizeUtil {
15
+
16
+    private static final int ERROR = -1;
17
+    private static DecimalFormat fileIntegerFormat = new DecimalFormat("#0");
18
+    private static DecimalFormat fileDecimalFormat = new DecimalFormat("#0.#");
19
+
20
+    /**
21
+     * SDCARD是否存
22
+     */
23
+    public static boolean externalMemoryAvailable() {
24
+        return android.os.Environment.getExternalStorageState().equals(
25
+                android.os.Environment.MEDIA_MOUNTED);
26
+    }
27
+
28
+    /**
29
+     * 获取手机内部剩余存储空间
30
+     *
31
+     * @return
32
+     */
33
+    public static long getAvailableInternalMemorySize() {
34
+        File path = Environment.getDataDirectory();
35
+        StatFs stat = new StatFs(path.getPath());
36
+        long blockSize = stat.getBlockSize();
37
+        long availableBlocks = stat.getAvailableBlocks();
38
+        return availableBlocks * blockSize;
39
+    }
40
+
41
+    /**
42
+     * 获取手机内部总的存储空间
43
+     *
44
+     * @return
45
+     */
46
+    public static long getTotalInternalMemorySize() {
47
+        File path = Environment.getDataDirectory();
48
+        StatFs stat = new StatFs(path.getPath());
49
+        long blockSize = stat.getBlockSizeLong();
50
+        long totalBlocks = stat.getBlockCountLong();
51
+        return totalBlocks * blockSize;
52
+    }
53
+
54
+    /**
55
+     * 获取SDCARD剩余存储空间
56
+     *
57
+     * @return
58
+     */
59
+    public static long getAvailableExternalMemorySize() {
60
+        if (externalMemoryAvailable()) {
61
+            File path = Environment.getExternalStorageDirectory();
62
+            StatFs stat = new StatFs(path.getPath());
63
+            long blockSize = stat.getBlockSize();
64
+            long availableBlocks = stat.getAvailableBlocks();
65
+            return availableBlocks * blockSize;
66
+        } else {
67
+            return ERROR;
68
+        }
69
+    }
70
+
71
+    /**
72
+     * 获取SDCARD总的存储空间
73
+     *
74
+     * @return
75
+     */
76
+    public static long getTotalExternalMemorySize() {
77
+        if (externalMemoryAvailable()) {
78
+            File path = Environment.getExternalStorageDirectory();
79
+            StatFs stat = new StatFs(path.getPath());
80
+            long blockSize = stat.getBlockSize();
81
+            long totalBlocks = stat.getBlockCount();
82
+            return totalBlocks * blockSize;
83
+        } else {
84
+            return ERROR;
85
+        }
86
+    }
87
+
88
+    /**
89
+     * 获取系统总内存
90
+     *
91
+     * @param context 可传入应用程序上下文。
92
+     * @return 总内存大单位为B。
93
+     */
94
+    public static long getTotalMemorySize(Context context) {
95
+        String dir = "/proc/meminfo";
96
+        try {
97
+            FileReader fr = new FileReader(dir);
98
+            BufferedReader br = new BufferedReader(fr, 2048);
99
+            String memoryLine = br.readLine();
100
+            String subMemoryLine = memoryLine.substring(memoryLine.indexOf("MemTotal:"));
101
+            br.close();
102
+            return Integer.parseInt(subMemoryLine.replaceAll("\\D+", "")) * 1024l;
103
+        } catch (IOException e) {
104
+            e.printStackTrace();
105
+        }
106
+        return 0;
107
+    }
108
+
109
+    /**
110
+     * 获取当前可用内存,返回数据以字节为单位。
111
+     *
112
+     * @param context 可传入应用程序上下文。
113
+     * @return 当前可用内存单位为B。
114
+     */
115
+    public static long getAvailableMemory(Context context) {
116
+        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
117
+        ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
118
+        am.getMemoryInfo(memoryInfo);
119
+        return memoryInfo.availMem;
120
+    }
121
+
122
+    /**
123
+     * 单位换算
124
+     *
125
+     * @param size      单位为B
126
+     * @param isInteger 是否返回取整的单位
127
+     * @return 转换后的单位
128
+     */
129
+    public static String formatFileSize(long size, boolean isInteger) {
130
+        DecimalFormat df = isInteger ? fileIntegerFormat : fileDecimalFormat;
131
+        String fileSizeString = "0M";
132
+        if (size < 1024 && size > 0) {
133
+            fileSizeString = df.format((double) size) + "B";
134
+        } else if (size < 1024 * 1024) {
135
+            fileSizeString = df.format((double) size / 1024) + "K";
136
+        } else if (size < 1024 * 1024 * 1024) {
137
+            fileSizeString = df.format((double) size / (1024 * 1024)) + "M";
138
+        } else {
139
+            fileSizeString = df.format((double) size / (1024 * 1024 * 1024)) + "G";
140
+        }
141
+        return fileSizeString;
142
+    }
143
+}

+ 17
- 0
corelib/src/main/java/com/xhly/corelib/utils/FileSizeUtils.kt Прегледај датотеку

@@ -0,0 +1,17 @@
1
+package com.xhly.corelib.utils
2
+
3
+import android.content.Context
4
+import android.os.Environment
5
+import android.os.StatFs
6
+
7
+class FileSizeUtils {
8
+    companion object{
9
+        fun getTotalSpace(context: Context): Long {
10
+            return context.filesDir.totalSpace
11
+        }
12
+
13
+        fun getFreeSpace(context: Context): Long {
14
+            return context.filesDir.freeSpace
15
+        }
16
+    }
17
+}

+ 71
- 0
corelib/src/main/java/com/xhly/corelib/utils/PhoneInfoUtils.java Прегледај датотеку

@@ -0,0 +1,71 @@
1
+package com.xhly.corelib.utils;
2
+import android.Manifest;
3
+import android.annotation.SuppressLint;
4
+import android.content.Context;
5
+import android.content.pm.PackageManager;
6
+import android.telephony.TelephonyManager;
7
+import androidx.core.app.ActivityCompat;
8
+
9
+
10
+/**
11
+ * Created by WangJinyong on 2018/3/31.
12
+ * 获取SIM卡信息和手机号码
13
+ */
14
+
15
+public class PhoneInfoUtils {
16
+
17
+    private static String TAG = "PhoneInfoUtils";
18
+    private TelephonyManager telephonyManager;
19
+    //移动运营商编号
20
+    private String NetworkOperator;
21
+    private Context context;
22
+
23
+    public PhoneInfoUtils(Context context) {
24
+        this.context = context;
25
+        telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
26
+    }
27
+
28
+    //获取sim卡iccid
29
+    public String getIccid() {
30
+        String iccid = "N/A";
31
+        iccid = telephonyManager.getSimSerialNumber();
32
+        return iccid;
33
+    }
34
+
35
+    //获取电话号码
36
+    @SuppressLint("MissingPermission")
37
+    public String getNativePhoneNumber(Context context) {
38
+        String nativePhoneNumber = "N/A";
39
+        nativePhoneNumber = telephonyManager.getLine1Number();
40
+        return nativePhoneNumber;
41
+    }
42
+
43
+    //获取手机服务商信息
44
+    public String getProvidersName() {
45
+        String providersName = "N/A";
46
+        NetworkOperator = telephonyManager.getNetworkOperator();
47
+        //IMSI号前面3位460是国家,紧接着后面2位00 02是中国移动,01是中国联通,03是中国电信。
48
+//        Flog.d(TAG,"NetworkOperator=" + NetworkOperator);
49
+        if (NetworkOperator.equals("46000") || NetworkOperator.equals("46002")) {
50
+            providersName = "中国移动";//中国移动
51
+        } else if (NetworkOperator.equals("46001")) {
52
+            providersName = "中国联通";//中国联通
53
+        } else if (NetworkOperator.equals("46003")) {
54
+            providersName = "中国电信";//中国电信
55
+        }
56
+        return providersName;
57
+    }
58
+
59
+    @SuppressLint("MissingPermission")
60
+    public  String getPhoneInfo(Context context) {
61
+        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
62
+        StringBuffer sb = new StringBuffer();
63
+        sb.append("\nLine1Number = " + tm.getLine1Number());
64
+        sb.append("\nNetworkOperator = " + tm.getNetworkOperator());//移动运营商编号
65
+        sb.append("\nNetworkOperatorName = " + tm.getNetworkOperatorName());//移动运营商名称
66
+        sb.append("\nSimCountryIso = " + tm.getSimCountryIso());
67
+        sb.append("\nSimOperator = " + tm.getSimOperator());
68
+        sb.append("\nSimOperatorName = " + tm.getSimOperatorName());
69
+        return sb.toString();
70
+    }
71
+}

+ 77
- 6
corelib/src/main/java/com/xhly/corelib/utils/SystemUtil.java Прегледај датотеку

@@ -1,9 +1,14 @@
1 1
 package com.xhly.corelib.utils;
2 2
 
3
+import android.content.Context;
3 4
 import android.os.Build;
5
+import android.telephony.SubscriptionInfo;
6
+import android.telephony.SubscriptionManager;
4 7
 import android.text.TextUtils;
5 8
 
9
+import java.io.File;
6 10
 import java.lang.reflect.Method;
11
+import java.util.List;
7 12
 import java.util.Locale;
8 13
 
9 14
 /**
@@ -26,7 +31,7 @@ public class SystemUtil {
26 31
     /**
27 32
      * 获取当前系统上的语言列表(Locale列表)
28 33
      *
29
-     * @return  语言列表
34
+     * @return 语言列表
30 35
      */
31 36
     public static Locale[] getSystemLanguageList() {
32 37
         return Locale.getAvailableLocales();
@@ -35,10 +40,10 @@ public class SystemUtil {
35 40
     /**
36 41
      * 获取当前手机系统版本号
37 42
      *
38
-     * @return  系统版本号
43
+     * @return 系统版本号
39 44
      */
40 45
     public static String getSystemVersion() {
41
-        String version ="";
46
+        String version = "";
42 47
         if (isHarmonyOs()) {
43 48
             version = "Harmony    " + getHarmonyVersion();
44 49
         } else {
@@ -50,7 +55,7 @@ public class SystemUtil {
50 55
     /**
51 56
      * 获取手机型号
52 57
      *
53
-     * @return  手机型号
58
+     * @return 手机型号
54 59
      */
55 60
     public static String getSystemModel() {
56 61
         return Build.MODEL;
@@ -60,7 +65,7 @@ public class SystemUtil {
60 65
     /**
61 66
      * 获取手机版本号
62 67
      *
63
-     * @return  手机型号
68
+     * @return 手机型号
64 69
      */
65 70
     public static String getSystemDisplay() {
66 71
         return Build.DISPLAY;
@@ -69,7 +74,7 @@ public class SystemUtil {
69 74
     /**
70 75
      * 获取手机厂商
71 76
      *
72
-     * @return  手机厂商
77
+     * @return 手机厂商
73 78
      */
74 79
     public static String getDeviceBrand() {
75 80
         return Build.BRAND;
@@ -126,4 +131,70 @@ public class SystemUtil {
126 131
     }
127 132
 
128 133
 
134
+    /**
135
+     * 是否存在su命令,并且有执行权限
136
+     *
137
+     * @return 存在su命令,并且有执行权限返回true
138
+     */
139
+    public static boolean isSuEnable() {
140
+        File file = null;
141
+        String[] paths = {"/system/bin/", "/system/xbin/", "/system/sbin/", "/sbin/", "/vendor/bin/", "/su/bin/"};
142
+        try {
143
+            for (String path : paths) {
144
+                file = new File(path + "su");
145
+                if (file.exists() && file.canExecute()) {
146
+                    return true;
147
+                }
148
+            }
149
+        } catch (Exception x) {
150
+            x.printStackTrace();
151
+        }
152
+        return false;
153
+    }
154
+
155
+    /**
156
+     * 获得系统内核版本
157
+     *
158
+     * @return
159
+     */
160
+
161
+    public static String getKernelVersion() {
162
+        String kernelVersion = "";
163
+        try {
164
+            kernelVersion = System.getProperty("os.version");
165
+        } catch (Exception e) {
166
+
167
+        }
168
+        return kernelVersion;
169
+    }
170
+
171
+    public static String getICCID(Context context) {
172
+        String iccid = "";
173
+        try{
174
+            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
175
+                SubscriptionManager sm = SubscriptionManager.from(context);
176
+                List<SubscriptionInfo> sis = sm.getActiveSubscriptionInfoList();
177
+                if (sis.size() >= 1) {
178
+                    SubscriptionInfo si1 = sis.get(0);
179
+                    String iccId1 = si1.getIccId();
180
+                    String phoneNum1 = si1.getNumber();
181
+                    iccid=iccId1;
182
+                }
183
+                if (sis.size() >= 2) {
184
+                    SubscriptionInfo si2 = sis.get(1);
185
+                    String iccId2 = si2.getIccId();
186
+                    String phoneNum2 = si2.getNumber();
187
+                    iccid=iccId2;
188
+                }
189
+                // 获取SIM卡数量相关信息:
190
+                int count = sm.getActiveSubscriptionInfoCount();//当前实际插卡数量
191
+                int max = sm.getActiveSubscriptionInfoCountMax();//当前卡槽数量
192
+            }
193
+        }catch (Exception e){
194
+
195
+        }
196
+        return iccid;
197
+    }
198
+
199
+
129 200
 }

Loading…
Откажи
Сачувај