安卓开发获取安卓设备信息工具类

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
以下是一个获取安卓设备信息的工具类:
 
```
import android.content.Context;
import android.os.Build;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;
 
public class DeviceInfoUtils {
 
    /**
     * 获取设备唯一标识符
     * @return
     */
    public static String getDeviceUniqueId(Context context) {
        String uniqueId = null;
        TelephonyManager telephonyManager = null;
        try {
            telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            uniqueId = telephonyManager.getDeviceId();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (uniqueId == null) {
            uniqueId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
        }
        if (uniqueId == null) {
            uniqueId = UUID.randomUUID().toString();
        }
        MessageDigest sha;
        try {
            sha = MessageDigest.getInstance("SHA-1");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return uniqueId;
        }
        byte[] hash = sha.digest(uniqueId.getBytes());
        StringBuilder sb = new StringBuilder();
        for (byte b : hash) {
            sb.append(Integer.toHexString(b));
        }
        return sb.toString();
    }
 
    /**
     * 获取设备名称
     * @return
     */
    public static String getDeviceName() {
        return Build.MODEL;
    }
 
    /**
     * 获取设备系统版本
     * @return
     */
    public static String getDeviceOsVersion() {
        return Build.VERSION.RELEASE;
    }
}
```
在使用调用该工具类获取设备信息,例如:
 
```
String uniqueId = DeviceInfoUtils.getDeviceUniqueId(context);
String deviceName = DeviceInfoUtils.getDeviceName();
String deviceOsVersion = DeviceInfoUtils.getDeviceOsVersion();
```
请注意,不同的设备可能会返回不同的设备标识符,这个标识符也可能会随设备的恢复出厂设置而变化。因此,获取设备唯一标识符时需要谨慎处理。

  

posted @   今年我十八岁  阅读(88)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示