服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android获取手机信息的工具类

Android获取手机信息的工具类

2022-08-07 13:36676598624 Android

这篇文章主要为大家详细介绍了Android获取手机信息的工具类,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

网上收集的一些获取收集信息的代码,制作成一个工具类,以后可以方便调用。

 

?
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
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
 
import android.app.Activity;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.format.Formatter;
 
/**
 * 获取手机信息工具类
 *
 */
public class PhoneUtil {
 
  private static PhoneUtil instance;
 
  private TelephonyManager tm;
  private Activity act;
 
  private PhoneUtil(Activity act) {
    tm = (TelephonyManager) act.getSystemService(Context.TELEPHONY_SERVICE);
    this.act = act;
  }
 
  public static PhoneUtil getInstance(Activity act) {
    if (instance == null) {
      instance = new PhoneUtil(act);
    } else if (instance.act != act) {
      instance = new PhoneUtil(act);
    }
    return instance;
  }
 
  /** 是否处于飞行模式 */
  public boolean isAirModeOpen() {
    return (Settings.System.getInt(act.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0) == 1 ? true
        : false);
  }
 
  /** 获取手机号码 */
  public String getPhoneNumber() {
    return tm == null ? null : tm.getLine1Number();
  }
 
  /** 获取网络类型(暂时用不到) */
  public int getNetWorkType() {
    return tm == null ? 0 : tm.getNetworkType();
  }
 
  /** 获取手机sim卡的序列号(IMSI) */
  public String getIMSI() {
    return tm == null ? null : tm.getSubscriberId();
  }
 
  /** 获取手机IMEI */
  public String getIMEI() {
    return tm == null ? null : tm.getDeviceId();
  }
 
  /** 获取手机型号 */
  public static String getModel() {
    return android.os.Build.MODEL;
  }
 
  /** 获取手机品牌 */
  public static String getBrand() {
    return android.os.Build.BRAND;
  }
 
  /** 获取手机系统版本 */
  public static String getVersion() {
    return android.os.Build.VERSION.RELEASE;
  }
 
  /** 获得手机系统总内存 */
  public String getTotalMemory() {
    String str1 = "/proc/meminfo";// 系统内存信息文件
    String str2;
    String[] arrayOfString;
    long initial_memory = 0;
 
    try {
      FileReader localFileReader = new FileReader(str1);
      BufferedReader localBufferedReader = new BufferedReader(localFileReader, 8192);
      str2 = localBufferedReader.readLine();// 读取meminfo第一行,系统总内存大小
 
      arrayOfString = str2.split("\\s+");
 
      initial_memory = Integer.valueOf(arrayOfString[1]).intValue() * 1024;// 获得系统总内存,单位是KB,乘以1024转换为Byte
      localBufferedReader.close();
 
    } catch (IOException e) {
    }
    return Formatter.formatFileSize(act, initial_memory);// Byte转换为KB或者MB,内存大小规格化
  }
 
  /** 获取手机屏幕宽 */
  public int getScreenWidth() {
    return act.getWindowManager().getDefaultDisplay().getWidth();
  }
 
  /** 获取手机屏高宽 */
  public int getScreenHeight() {
    return act.getWindowManager().getDefaultDisplay().getHeight();
  }
 
  /** 获取应用包名 */
  public String getPackageName() {
    return act.getPackageName();
  }
 
  /**
   * 获取手机MAC地址 只有手机开启wifi才能获取到mac地址
   */
  public String getMacAddress() {
    String result = "";
    WifiManager wifiManager = (WifiManager) act.getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    result = wifiInfo.getMacAddress();
    return result;
  }
 
  /**
   * 获取手机CPU信息 //1-cpu型号 //2-cpu频率
   */
  public String[] getCpuInfo() {
    String str1 = "/proc/cpuinfo";
    String str2 = "";
    String[] cpuInfo = { "", "" }; // 1-cpu型号 //2-cpu频率
    String[] arrayOfString;
    try {
      FileReader fr = new FileReader(str1);
      BufferedReader localBufferedReader = new BufferedReader(fr, 8192);
      str2 = localBufferedReader.readLine();
      arrayOfString = str2.split("\\s+");
      for (int i = 2; i < arrayOfString.length; i++) {
        cpuInfo[0] = cpuInfo[0] + arrayOfString[i] + " ";
      }
      str2 = localBufferedReader.readLine();
      arrayOfString = str2.split("\\s+");
      cpuInfo[1] += arrayOfString[2];
      localBufferedReader.close();
    } catch (IOException e) {
    }
    return cpuInfo;
  }
 
  /** 获取Application中的meta-data内容 */
  public String getMetaData(String name) {
    String result = "";
    try {
      ApplicationInfo appInfo = act.getPackageManager().getApplicationInfo(getPackageName(),
          PackageManager.GET_META_DATA);
      result = appInfo.metaData.getString(name);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return result;
  }
 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_26761229/article/details/54965969

延伸 · 阅读

精彩推荐