Runt
2022-03-29 9aeecdbe43d4c4601710b6c7b301f41f55c07746
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package com.auto.lyric.util;
 
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Point;
import android.os.Build;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;
 
import androidx.core.app.ActivityCompat;
 
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
 
 
/**
 * Created by EDZ on 2018/1/30.
 */
public class DeviceUtil {
    public static final String TAG = "DeviceUtil";
 
    /**
     * 设备信息
     *
     * @param context
     */
    public static void getDisplay(Context context) {
        StringBuilder sb = new StringBuilder();
        sb.append("Version code is  \n");
        sb.append("设备的Android版本号:");//设备的Android版本号
        sb.append(getSDKInt() + " " + getSDKVersion() + "\t");//设备的Android版本号
        sb.append("设备型号:");//设备型号
        sb.append(getDeviceModel() + "\t");//设备型号
        sb.append("设备厂商:");//设备型号
        sb.append(getDeviceBrand() + "\t");//设备型号
        sb.append("程序版本号:" + getAppVersionCode(context) + " " + getAppVersionName(context) + "\t");//程序版本号
        sb.append("设备唯一标识符:" + getSerialNumber(context));
        sb.append("\n设备imei:" + getIMEI(context));
        String str = sb.toString() + " \n";
        str += getDisplayInfomation(context) + " \n";
        str += getDensity(context) + " \n";
        str += "屏幕大小:" + getScreenInch(context) + "英寸 \n";
        str += getAndroiodScreenProperty(context) + "\n";
        Log.i(TAG, str);
    }
 
    /**
     * Double类型保留指定位数的小数,返回double类型(四舍五入)
     * newScale 为指定的位数
     */
    private static double formatDouble(double d, int newScale) {
        BigDecimal bd = new BigDecimal(d);
        return bd.setScale(newScale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
 
    /**
     * 设备型号
     * @return
     */
    public static String getDeviceModel() {
        return Build.MODEL;
    }
 
    /**
     * 设备厂商
     * @return
     */
    public static String getDeviceBrand() {
        return Build.BRAND;
    }
 
    /**
     * 设备的Android版本号
     * @return
     */
    public static int getSDKInt() {
        return Build.VERSION.SDK_INT;
    }
 
    /**
     * 设备的Android版本号
     * @return
     */
    public static String getSDKVersion() {
        return Build.VERSION.RELEASE;
    }
 
    /**
     * 程序版本号
     * @param context
     * @return
     */
    public static int getAppVersionCode(Context context) {
        try {
            return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return 1;
    }
 
    /**
     * 程序版本号
     *
     * @param context
     * @return
     */
    public static String getAppVersionName(Context context) {
        try {
            return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return "";
    }
 
    /**
     * 屏幕像素px
     */
    @SuppressLint("NewApi")
    public static String getDisplayInfomation(Context context) {
        Point point = new Point();
        ((Activity) context).getWindowManager().getDefaultDisplay().getSize(point);
        Log.d(TAG, "the screen size is " + point.toString());
        ((Activity) context).getWindowManager().getDefaultDisplay().getRealSize(point);
        Log.d(TAG, "the screen real size is " + point.toString());
        return point.toString();
    }
 
    /**
     * 屏幕信息
     * @param context
     * @return
     */
    public static String getAndroiodScreenProperty(Context context) {
        int width = getScreenPixel(context).widthPixels;         // 屏幕宽度(像素)
        int height = getScreenPixel(context).heightPixels;       // 屏幕高度(像素)
        float density = getDensity(getScreenPixel(context));         // 屏幕密度(0.75 / 1.0 / 1.5)
        int densityDpi = getDpi(context);     // 屏幕密度dpi(120 / 160 / 240)
        // 屏幕宽度算法:屏幕宽度(像素)/屏幕密度
        int screenWidth = getDp(context)[0];  // 屏幕宽度(dp)
        int screenHeight = getDp(context)[1];// 屏幕高度(dp)
 
 
        Log.d("h_bl", "屏幕宽度(像素):" + width);
        Log.d("h_bl", "屏幕高度(像素):" + height);
        Log.d("h_bl", "屏幕密度(0.75 / 1.0 / 1.5):" + density);
        Log.d("h_bl", "屏幕密度dpi(120 / 160 / 240):" + densityDpi);
        Log.d("h_bl", "屏幕宽度(dp):" + screenWidth);  // 屏幕适配文件夹(例:layout-sw300dp),是以该属性为准则
        Log.d("h_bl", "屏幕高度(dp):" + screenHeight);
 
        StringBuilder sb = new StringBuilder();
        sb.append("屏幕宽度(像素):" + width + "\n");
        sb.append("屏幕高度(像素):" + height + "\n");
        sb.append("屏幕密度(0.75 / 1.0 / 1.5):" + density + "\n");
        sb.append("屏幕密度dpi(120 / 160 / 240):" + densityDpi + "\n");
        sb.append("屏幕宽度(dp):" + screenWidth + "\n");
        sb.append("屏幕高度(dp):" + screenHeight + "\n");
        return sb.toString();
 
    }
 
    /**
     * 通知栏高度
     * @param context
     * @return
     */
    public static int getStatusBarHeight(Context context) {
        Class<?> c = null;
        Object obj = null;
        Field field = null;
        int x = 0, statusBarHeight = 0;
        try {
            c = Class.forName("com.android.internal.R$dimen");
            obj = c.newInstance();
            field = c.getField("status_bar_height");
            x = Integer.parseInt(field.get(obj).toString());
            statusBarHeight = context.getResources().getDimensionPixelSize(x);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return statusBarHeight;
    }
 
    /**
     * 屏幕密度dpi
     */
    public static int getDpi(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        return dm.densityDpi;
    }
 
 
    /**
     * 屏幕像素px
     */
    @SuppressLint("NewApi")
    public static Point getDisplayPixel(Context context) {
        Point point = new Point();
        ((Activity) context).getWindowManager().getDefaultDisplay().getSize(point);
        Log.d(TAG, "the screen size is " + point.toString());
        ((Activity) context).getWindowManager().getDefaultDisplay().getRealSize(point);
        Log.d(TAG, "the screen real size is " + point.toString());
        return point;
    }
 
    /**
     * 屏幕像素px
     */
    public static DisplayMetrics getScreenPixel(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        return dm;
    }
 
    /**
     * 屏幕dp
     */
    public static int[] getDp(Context context) {
        int width = getScreenPixel(context).widthPixels;         // 屏幕宽度(像素)
        int height = getScreenPixel(context).heightPixels;       // 屏幕高度(像素)
        float density = getDensity(getScreenPixel(context));         // 屏幕密度(0.75 / 1.0 / 1.5)
        // 屏幕宽度算法:屏幕宽度(像素)/屏幕密度
        int screenWidth = (int) (width / density);  // 屏幕宽度(dp)
        int screenHeight = (int) (height / density);// 屏幕高度(dp)
        return new int[]{screenWidth, screenHeight};
    }
 
    /**
     * 屏幕密度
     */
    public static float getDensity(DisplayMetrics dm) {
        return dm.density;         // 屏幕密度(0.75 / 1.0 / 1.5);
    }
 
    /**
     * 屏幕密度dpi
     */
    public static String getDensity(Context context) {
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        Log.d(TAG, "Density is " + displayMetrics.density + " densityDpi is " + displayMetrics.densityDpi + " height: " + displayMetrics.heightPixels +
                " width: " + displayMetrics.widthPixels);
        return "Density is " + displayMetrics.density + " densityDpi is " + displayMetrics.densityDpi + " height: " + displayMetrics.heightPixels +
                " width: " + displayMetrics.widthPixels;
    }
 
 
    /**
     * 屏幕尺寸inch
     */
    public static double getScreenInch(Context context) {
 
        try {
            int realWidth = 0, realHeight = 0;
            Display display = ((Activity) context).getWindowManager().getDefaultDisplay();
            DisplayMetrics metrics = new DisplayMetrics();
            display.getMetrics(metrics);
            if (Build.VERSION.SDK_INT >= 17) {
                Point size = new Point();
                display.getRealSize(size);
                realWidth = size.x;
                realHeight = size.y;
            } else if (Build.VERSION.SDK_INT < 17
                    && Build.VERSION.SDK_INT >= 14) {
                Method mGetRawH = Display.class.getMethod("getRawHeight");
                Method mGetRawW = Display.class.getMethod("getRawWidth");
                realWidth = (Integer) mGetRawW.invoke(display);
                realHeight = (Integer) mGetRawH.invoke(display);
            } else {
                realWidth = metrics.widthPixels;
                realHeight = metrics.heightPixels;
            }
 
            return formatDouble(Math.sqrt((realWidth / metrics.xdpi) * (realWidth / metrics.xdpi) + (realHeight / metrics.ydpi) * (realHeight / metrics.ydpi)), 1);
 
 
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        return 0;
    }
 
    /**
     * dp获取dip
     * @param dp
     * @return
     */
    public static int convertDpToPixel(float dp, Context context) {
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        return (int) (dp * displayMetrics.density);
    }
 
    /***
     * px获取dip
     * @param pixel
     * @return
     */
    public static int convertPixelToDp(int pixel, Context context) {
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        return (int) (pixel / displayMetrics.density);
    }
 
    public static String getSerialNumber(Context context) {
        String serial = "";
        try {
            if (Build.VERSION.SDK_INT >= 28) {//9.0+
                if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                    Log.i(TAG, "getMEID meid: READ_PHONE_STATE" );
                    ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.READ_PHONE_STATE}, 1567);
                } else {
                    serial = Build.getSerial();
                }
            } else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N) {//8.0+
                serial = Build.SERIAL;
            } else {//8.0-
                Class<?> c = Class.forName("android.os.SystemProperties");
                Method get = c.getMethod("get", String.class);
                serial = (String) get.invoke(c, "ro.serialno");
            }
        } catch (Exception e) {
            Log.e("e", "读取设备序列号异常:" + e.toString());
        }
        return serial;
    }
 
    public static  String getIMEI(Context context) {
        String deviceId = "";
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if (null != tm) {
            if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                Log.i(TAG, "getMEID meid: READ_PHONE_STATE" );
                ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.READ_PHONE_STATE}, 1567);
            } else {
                if (tm.getDeviceId() != null) {
                    deviceId = tm.getDeviceId();
                } else {
                    deviceId = Settings.Secure.getString(context.getApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID);
                }
            }
            Log.d("deviceId--->", deviceId);
        }
        return "";
    }
 
    public static  String getMEID() {
        try {
            Class clazz = Class.forName("android.os.SystemProperties");
            Method method = clazz.getMethod("get", String.class, String.class);
 
            String meid = (String) method.invoke(null, "ril.cdma.meid", "");
            if (!TextUtils.isEmpty(meid)) {
                Log.d(TAG, "getMEID meid: " + meid);
                return meid;
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.w(TAG, "getMEID error : " + e.getMessage());
        }
        return "";
    }
}