`
chriszeng87
  • 浏览: 720204 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Android获取cpu和内存信息、网址的代码

阅读更多
	/** 获取用户硬件信息 */
	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 = new BufferedReader(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;
	}
 

 

0
0
分享到:
评论
1 楼 songshuaiyang 2017-03-01  
哥们写东西可真乱啊 

相关推荐

    android 获取cpu使用率, 内存 实时数据

    可以获取系统的内存和可用内存,cpu的实时使用率

    Android编程实现获取系统内存、CPU使用率及状态栏高度的方法示例

    本文实例讲述了Android编程实现获取系统内存、CPU使用率及状态栏高度的方法。分享给大家供大家参考,具体如下: DeviceInfoManage类用于获取系统的内存,CPU的信息,以及状态栏的高度 import java.io.BufferedReader...

    Android代码-查看手机应用的CPU和内存资源的使用情况

    Due to undocumented changes made by Google, on Android 7.0 CPU usage information for processes others than the own AnotherMonitor one will not be available (not even with root access). Rest of the app...

    Python获取android设备cpu和内存占用情况

    使用方法:使用adb连接android设备,打开将要测试的app,执行cpu/内存代码 cpu获取代码如下:(输入参数为脚本执行时间) # coding:utf-8 ''' 获取系统total cpu ''' import os, csv import time import csv import...

    解析Android获取系统cpu信息,内存,版本,电量等信息的方法详解

    Android获取系统cpu信息,内存,版本,电量等信息 1、CPU频率,CPU信息:/proc/cpuinfo和/proc/stat 通过读取文件/proc/cpuinfo系统CPU的类型等多种信息。读取/proc/stat 所有CPU活动的信息来计算CPU使用率 下面我们...

    Android获取设备CPU核数、时钟频率以及内存大小的方法

    本文实例讲述了Android获取设备CPU核数、时钟频率以及内存大小的方法。分享给大家供大家参考,具体如下: 因项目需要,分析了一下 Facebook 的开源项目 – Device Year Class。 Device Year Class 的主要功能是根据 ...

    获取 Android 设备的系统信息 包含屏幕信息、内存信息、IMEI、存储、传感器、CPU等信息 .zip

    对于有一定基础或热衷于研究的人来说,可以在这些基础代码上进行修改和扩展,实现其他功能。 【沟通交流】: 有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 鼓励下载和使用,并欢迎大家互相学习,共同...

    Android代码-QPM

    可以实时获取当前 App 的 CPU 和内存信息,可以判断手机是否占用更多资源; 界面卡不卡,就看 FPS。 绿色表示正常,红色表示卡顿; 轻松获取 当前运行的 Activity,快速定位界面。 App 已经开启多少线程,是否占用...

    native.js获取手机硬件基本信息实例代码android版

    为大家分享一些android公共方法native.js实现代代码,如获取手机MAC地址,手机内存大小,手机存储空间大小,手机CPU信息等手机硬件基本信息 native.js获取手机MAC地址 /*得到手机MAC地址*/ function getMac() { var ...

    Android代码-wlmusic

    wlmusic v1.2.6(讨论群:806397913) 基于FFmpeg OpenSL ES的音频播放SDK。可循环不间断播放短音频;播放raw和assets音频文件;可独立设置音量大小;可实时现在音量分贝大小(用于绘制波形图...CPU和内存使用情况:测

    Android 获取手机信息实例详解

    手机信息:手机屏幕宽和高、当前可用内存大小、总内存大小、IMEI号、IESI号、手机型号、手机品牌、手机MacAdd、CPU型号、CPU频率 开门见山,以下是Java代码,XML只有一个TextView显示信息。 package ...

    Android最新资料以及源码

    共20个目标文件,CPU管理、内存管理、文件操作、进程监视管理、获取机子信息……等等。 10、Android触摸界面产生气泡的源码 如题,共1个目标文件。 11、Android读写文件源码 共1个目标文件,文件操作! 12、...

    新版Android开发教程.rar

    � 采用了对有限内存、电池和 CPU 优化过的虚拟机 Dalvik , Android 的运行速度比想象的要快很多。 � 运营商(中国移动等)的大力支持,产业链条的热捧。 � 良好的盈利模式( 3/7 开),产业链条的各方:运营商、...

    AndroidCpuTools:CPU 调试工具 CPU tools show information

    因隐藏类SDK获取不到,故需要在Android源码下编译才可以正常编译本工程作用作为代码参考,目录有 CpuRunTools.apk,方便自行安装与看效果目前Android各个系统版本在CPU的接口控制上变动性各有千秋,其他系统版本情况...

    基于android的视频播放器---开题报告.doc

    于是各种手机播放器也紧跟着发展起来,但是很多播放器一味追求外" "观花哨,功能庞大,对用户的手机造成了很多资源浪费,比如CPU,内存等的占用 " "率过高,在用户需要多任务操作时,受到了不小的影响,带来了许多...

    JAVA上百实例源码以及开源项目源代码

    用累加器,以对话状态存储起来,创建EJB对象,并将当前的计数器初始化,调用每一个EJB对象的count()方法,保证Bean正常被激活和钝化,EJB对象是用完毕,从内存中清除…… Java Socket 聊天通信演示代码 2个目标文件...

    【IPC】Android中的进程间通信(IPC)详解

    1 需要的知识点 1.1 进程与线程 要理解跨进程通信,首先需要...为了加大一个应用可使用的内存,通过多进程来获取多份内存空间 开启多进程模式是通过在AndroidManifest中指定Android:process属性 以“:”开头的进程

Global site tag (gtag.js) - Google Analytics