android获取手机cpu并判断是单核还是多核

/** 
* Gets the number of cores available in this device, across all processors. 
* Requires: Ability to peruse the filesystem at "/sys/devices/system/cpu" 
* @return The number of cores, or 1 if failed to get result 
*/ 
private int getNumCores() { 
//Private Class to display only CPU devices in the directory listing 
class CpuFilter implements FileFilter { 
@Override 
public boolean accept(File pathname) { 
//Check if filename is "cpu", followed by a single digit number 
if(Pattern.matches("cpu[0-9]", pathname.getName())) { 
return true; 

return false; 

}

try { 
//Get directory containing CPU info 
File dir = new File("/sys/devices/system/cpu/"); 
//Filter to only list the devices we care about 
File[] files = dir.listFiles(new CpuFilter()); 
//Return the number of cores (virtual CPU devices) 
return files.length; 
} catch(Exception e) { 
//Default to return 1 core 
return 1; 

}

 
Android获取cpu和内存信息、网址的代码,/** 获取用户硬件信息 */
 
 
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
/** 获取用户硬件信息 */
public static String getMobileInfo() { 
    //StringBuffer sb = new StringBuffer(); 
    JSONObject mbInfo = new JSONObject(); 
        
    //通过反射获取用户硬件信息 
    try 
    
        Field[] fields = Build.class.getDeclaredFields(); 
        for (Field field : fields) { 
            // 暴力反射,获取私有信息 
            field.setAccessible(true); 
            String name = field.getName(); 
            String value = field.get(null).toString(); 
            //sb.append(name + "=" + value); 
            //sb.append("n"); 
            mbInfo.put(name, value); 
        
    catch (Exception e) { 
        e.printStackTrace(); 
    
        
    //return sb.toString(); 
    return mbInfo.toString(); 
    
    
static public String getCpuString(){ 
    if(Build.CPU_ABI.equalsIgnoreCase("x86")){ 
        return "Intel"
    
        
    String strInfo = ""
    try
    
        byte[] bs = new byte[1024]; 
        RandomAccessFile reader = new RandomAccessFile("/proc/cpuinfo","r"); 
        reader.read(bs); 
        String ret = new String(bs); 
        int index = ret.indexOf(0); 
        if(index != -1) { 
            strInfo = ret.substring(0, index); 
        else 
            strInfo = ret; 
        
    
    catch (IOException ex){ 
        ex.printStackTrace(); 
    
        
    return strInfo; 
    
static public String getCpuType(){ 
    String strInfo = getCpuString(); 
    String strType = null
        
    if (strInfo.contains("ARMv5")) { 
        strType = "armv5"
    else if (strInfo.contains("ARMv6")) { 
        strType = "armv6"
    else if (strInfo.contains("ARMv7")) { 
        strType = "armv7"
    else if (strInfo.contains("Intel")){ 
        strType = "x86"
    }else
        strType = "unknown"
        return strType; 
    
        
    if (strInfo.contains("neon")) { 
        strType += "_neon"
    }else if (strInfo.contains("vfpv3")) { 
        strType += "_vfpv3"
    }else if (strInfo.contains(" vfp")) { 
        strType += "_vfp"
    }else
        strType += "_none"
    
        
    return strType; 
    
    
    
/**
 * @hide
 * @return
 */
public static CPUInfo getCPUInfo() { 
    String strInfo = null
    try
    
        byte[] bs = new byte[1024]; 
        RandomAccessFile reader = new RandomAccessFile("/proc/cpuinfo","r"); 
        reader.read(bs); 
        String ret = new String(bs); 
        int index = ret.indexOf(0); 
        if(index != -1) { 
            strInfo = ret.substring(0, index); 
        else 
            strInfo = ret; 
        
    
    catch (IOException ex) 
    
        strInfo = ""
        ex.printStackTrace(); 
    
        
    CPUInfo info = parseCPUInfo(strInfo); 
    info.mCPUMaxFreq = getMaxCpuFreq(); 
                
    return info; 
    
    
private final static String kCpuInfoMaxFreqFilePath ="/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq"
private static int getMaxCpuFreq() { 
    int result = 0
    FileReader fr = null
    BufferedReader br = null
    try 
        fr = new FileReader(kCpuInfoMaxFreqFilePath); 
        br = new BufferedReader(fr); 
        String text = br.readLine(); 
        if (text != null) { 
            result = Integer.parseInt(text.trim()); 
        
    catch (FileNotFoundException e) { 
        e.printStackTrace(); 
    catch (IOException e) { 
        e.printStackTrace(); 
    finally 
        if (fr != null
            try 
                fr.close(); 
            catch (IOException e) { 
                // TODO Auto-generated catch block 
                e.printStackTrace(); 
            
        if (br != null
            try 
                br.close(); 
            catch (IOException e) { 
                // TODO Auto-generated catch block 
                e.printStackTrace(); 
            
    
    
    return result; 
        
public static class CPUInfo{ 
    public CPUInfo(){ 
            
    
        
    public static final int CPU_TYPE_UNKNOWN            =   0x00000000
    public static final int CPU_TYPE_ARMV5TE            =   0x00000001
    public static final int CPU_TYPE_ARMV6              =   0x00000010
    public static final int CPU_TYPE_ARMV7              =   0x00000100
        
    public static final int CPU_FEATURE_UNKNOWS         =   0x00000000
    public static final int CPU_FEATURE_VFP             =   0x00000001
    public static final int CPU_FEATURE_VFPV3           =   0x00000010
    public static final int CPU_FEATURE_NEON            =   0x00000100
        
    public int mCPUType; 
    public int mCPUCount; 
    public int mCPUFeature;      
    public double mBogoMips; 
    public long mCPUMaxFreq; 
    
/**
 
 * @param cpuInfo
 * @return
 * @hide
 */
private static CPUInfo parseCPUInfo(String cpuInfo) { 
    if (cpuInfo == null || "".equals(cpuInfo)) { 
        return null
    
    
    CPUInfo ci = new CPUInfo(); 
    ci.mCPUType = CPUInfo.CPU_TYPE_UNKNOWN; 
    ci.mCPUFeature = CPUInfo.CPU_FEATURE_UNKNOWS; 
    ci.mCPUCount = 1
    ci.mBogoMips = 0
    
    if (cpuInfo.contains("ARMv5")) { 
        ci.mCPUType = CPUInfo.CPU_TYPE_ARMV5TE; 
    else if (cpuInfo.contains("ARMv6")) { 
        ci.mCPUType = CPUInfo.CPU_TYPE_ARMV6; 
    else if (cpuInfo.contains("ARMv7")) { 
        ci.mCPUType = CPUInfo.CPU_TYPE_ARMV7; 
    
    
    if (cpuInfo.contains("neon")) { 
        ci.mCPUFeature |= CPUInfo.CPU_FEATURE_NEON; 
    
    
    if (cpuInfo.contains("vfpv3")) { 
        ci.mCPUFeature |= CPUInfo.CPU_FEATURE_VFPV3; 
    
    
    if (cpuInfo.contains(" vfp")) { 
        ci.mCPUFeature |= CPUInfo.CPU_FEATURE_VFP; 
    
    
    String[] items = cpuInfo.split("n"); 
    
    for (String item : items) { 
        if (item.contains("CPU variant")) { 
            int index = item.indexOf(": "); 
            if (index >= 0) { 
                String value = item.substring(index + 2); 
                try 
                    ci.mCPUCount = Integer.decode(value); 
                    ci.mCPUCount = ci.mCPUCount == 0 1 : ci.mCPUCount; 
                catch (NumberFormatException e) { 
                    ci.mCPUCount = 1
                
            
        else if (item.contains("BogoMIPS")) { 
            int index = item.indexOf(": "); 
            if (index >= 0) { 
                String value = item.substring(index + 2); 
            
        
    
        
    return ci; 
    
    
    
/**
 * 获取设备内存大小值
 * @return 内存大小,单位MB
 */
public static long getTotalMemory() {  
    String str1 = "/proc/meminfo"
    String str2;         
    String[] arrayOfString; 
    long initial_memory = 0
    try 
        FileReader localFileReader = new FileReader(str1); 
        BufferedReader localBufferedReader = newBufferedReader(localFileReader, 8192); 
        str2 = localBufferedReader.readLine(); 
        if (str2 != null) { 
            arrayOfString = str2.split("\s+"); 
            initial_memory = Integer.valueOf(arrayOfString[1]).intValue()/1024
        
        localBufferedReader.close(); 
        return initial_memory; 
    }  
    catch (IOException e)  
    {        
        return -1
    
    
    
/**
 * @hide
 * @return
 */
public CPUInfo getCPUInfo() { 
    String strInfo = null
    try
    
        byte[] bs = new byte[1024]; 
        RandomAccessFile reader = new RandomAccessFile("/proc/cpuinfo","r"); 
        reader.read(bs); 
        String ret = new String(bs); 
        int index = ret.indexOf(0); 
        if(index != -1) { 
            strInfo = ret.substring(0, index); 
        else 
            strInfo = ret; 
        
    
    catch (IOException ex) 
    
        strInfo = ""
        ex.printStackTrace(); 
    
        
    CPUInfo info = parseCPUInfo(strInfo); 
    info.mCPUMaxFreq = getMaxCpuFreq(); 
                
    return info; 
    
/**
 * 获取android CPU类型
 
 * @return String CPU类型
 */
public static String getCpuModel(){ 
    String cpu_model = ""
    
    CPUInfo in = getCPUInfo(); 
              
    if ((in.mCPUType & CPUInfo.CPU_TYPE_ARMV5TE) == CPUInfo.CPU_TYPE_ARMV5TE) 
        cpu_model="armv5"
    else if ((in.mCPUType & CPUInfo.CPU_TYPE_ARMV6) == CPUInfo.CPU_TYPE_ARMV6) 
        cpu_model="armv6"
    else if ((in.mCPUType & CPUInfo.CPU_TYPE_ARMV7) == CPUInfo.CPU_TYPE_ARMV7) 
        cpu_model="armv7"
    else
        cpu_model="unknown"
    return cpu_model; 
    
/**
 * 获取android CPU特性
 
 * @return String CPU特性
 */
public static String getCpuFeature(){ 
    String cpu_feature = ""
            
    CPUInfo in = getCPUInfo(); 
                
    if ((in.mCPUFeature & CPUInfo.CPU_FEATURE_NEON ) == CPUInfo.CPU_FEATURE_NEON) 
        cpu_feature="neon"
    else if ((in.mCPUFeature & CPUInfo.CPU_FEATURE_VFP ) == CPUInfo.CPU_FEATURE_VFP) 
        cpu_feature="vfp"
    else if ((in.mCPUFeature & CPUInfo.CPU_FEATURE_VFPV3 ) == CPUInfo.CPU_FEATURE_VFPV3) 
        cpu_feature="vfpv3"
    else
        cpu_feature="unknown";  
    return cpu_feature; 
    
/**
 * 获取ip地址
 
 * @param mContext  Context
 * @return ip地址字符串
 */
public static String getIpAddress(Context mContext) { 
    String ipAddress = null
    try 
        for (Enumeration<NetworkInterface> en = NetworkInterface 
                .getNetworkInterfaces(); en.hasMoreElements();) { 
            NetworkInterface intf = en.nextElement(); 
            for (Enumeration<InetAddress> enumIpAddr = intf 
                    .getInetAddresses(); enumIpAddr.hasMoreElements();) { 
                InetAddress inetAddress = enumIpAddr.nextElement(); 
                if (!inetAddress.isLoopbackAddress()) { 
                    ipAddress = inetAddress.getHostAddress().toString();  
                
            
        
    catch (SocketException ex) { 
        return null
    
    if (DEBUG) { 
        Log.d(TAG, "ip address:" + ipAddress); 
    
    return ipAddress; 
}

 

Android获取cpu和内存信息、网址的代码的更多相关文章

  1. 使用python获取CPU和内存信息的思路与实现(linux系统)

    linux里一切皆为文件,在linux/unix的根文件夹下,有个/proc文件夹,这个/proc 是一种内核和内核模块用来向进程(process)发送信息的机制(所以叫做"/proc&qu ...

  2. Android自动化测试-自动获取cpu和内存信息

    CpuInfo.java package com.dtest; import java.io.BufferedReader; import java.io.FileWriter; import jav ...

  3. Android 获取手机总内存和可用内存等信息

    在android开发中,有时候我们想获取手机的一些硬件信息,比如android手机的总内存和可用内存大小.这个该如何实现呢? 通过读取文件"/proc/meminfo"的信息能够获 ...

  4. 方法:Linux 下用JAVA获取CPU、内存、磁盘的系统资源信息

    CPU使用率: InputStream is = null; InputStreamReader isr = null; BufferedReader brStat = null; StringTok ...

  5. iOS开发 - 在状态栏显示FPS,CPU和内存信息

    原理 FPS的计算 CoreAnimation有一个很好用的类CADisplayLink,这个类会在每一帧绘制之前调用,并且可以获取时间戳.于是,我们只要统计出,在1s内的帧数即可. - (void) ...

  6. ubuntu系统中查看本机cpu和内存信息的命令和用法

    https://zhidao.baidu.com/question/192966322.html 写出ubuntu linux系统中查看本机cpu和内存信息的命令和用法,以及如何解读这些命令 ubun ...

  7. Linux下使用java获取cpu、内存使用率

    原文地址:http://www.voidcn.com/article/p-yehrvmep-uo.html 思路如下:Linux系统中可以用top命令查看进程使用CPU和内存情况,通过Runtime类 ...

  8. busybox devmem 直接获取、修改内存信息

    /********************************************************************** * busybox devmem 直接获取.修改内存信息 ...

  9. Python获取CPU、内存使用率以及网络使用状态代码

    Python获取CPU.内存使用率以及网络使用状态代码_python_脚本之家 http://www.jb51.net/article/134714.htm

随机推荐

  1. 生成订单唯一id

    $yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'); $orderSn = $yCode[intval(date('Y') ...

  2. dede导航设置成单页面内容

    有时顶级导航可能就是一个单页面 如公司简介 联系我们等 方法一:直接在导航栏填写内容 常规设置 二高级选项设置模板 三 填写页面内容 四 模板页面调用 内容 可在栏目模板中用{dede:field.c ...

  3. Java认证:JavaRunnable线程编写接口代码

    Java认证:JavaRunnable线程编写接口代码.JavaRunnable线程如何才能更好的适应目前的编程环境呢?下面我们就看看如何才能更好的进行相关环境.希望下面的文章对大家有所帮助.Java ...

  4. Python 的“+”和append在添加字符串时候的区别

    对于一个空的Python列表,往后添加内容有很多种,其中两种一个是用“+”直接添加内容,另外一种是Listname.append(x)来添加内容 其中,如果处理字符串 在使用“+”的时候,会将字符串拆 ...

  5. python学习笔记(一)元组,序列,字典

    python学习笔记(一)元组,序列,字典

  6. MySQL Explain 结果解读与实践

    Explain 结果解读与实践   基于 MySQL 5.0.67 ,存储引擎 MyISAM .   注:单独一行的"%%"及"`"表示分隔内容,就象分开&qu ...

  7. [BZOJ 1042] [HAOI2008] 硬币购物 【DP + 容斥】

    题目链接:BZOJ - 1042 题目分析 首先 Orz Hzwer ,代码题解都是看的他的 blog. 这道题首先使用DP预处理,先求出,在不考虑每种硬币个数的限制的情况下,每个钱数有多少种拼凑方案 ...

  8. matlab取整

    matlab取整 Matlab取整函数有: fix, floor, ceil, round.取整函数在编程时有很大用处.一.取整函数1.向零取整(截尾取整)fix-向零取整(Round towards ...

  9. Tiling(递推+大数)

    Description In how many ways can you tile a 2xn rectangle by 2x1 or 2x2 tiles? Here is a sample tili ...

  10. 使用SetWindowPos API函数移动窗口后,还需修改Delphi的属性值,以备下次使用,否则就会出问题(不是API不起作用,而是使用了错误的坐标值)

    单独改变坐标的代码如下,可不断左移: procedure TForm1.Button1Click(Sender: TObject); begin SetWindowPos(panel1.Handle, ...