码迷,mamicode.com
首页 > 移动开发 > 详细

android获取手机信息

时间:2014-12-02 22:44:55      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:android   linux指令解析器   手机信息   

android系统的很多信息可以通过 /proc 目录下获得,如

cat  /proc/cpuinfo                       获取cpu信息

cat /proc/meminfo                      获取内存信息

这些信息以文本格式保存,可以通过IO流读取,比较简单,在这里考虑到一些内容并不是以文本方式保存,磁盘信息

我们通过代码实现一个linux指令解析器来得到要获取的信息

指令解析器如下:

public class CMDExecutor {
	/**
	 * 执行命令
	 * @param cmd       命令参数
	 * @param workdir   当前目录(即执行指令 pwd 看到所在目录)
	 * @return
	 */
	public synchronized String run(String[] cmd, String workdir){
		String result = "";
		InputStream is = null;
		try {
			ProcessBuilder builder = new ProcessBuilder(cmd);
			if(!TextUtils.isEmpty(workdir)){
				builder.directory(new File(workdir));   // 设置执行指令执所在目录
				builder.redirectErrorStream();
				Process process = builder.start();      // 执行指令
				is = process.getInputStream();
				byte[] buf = new byte[1024];
				int len = 0;
				while((len = is.read(buf)) != -1){
					result += new String(buf, 0, len);
				}
			}
		} catch (Exception e) {
			return null;
		}finally{
			if(is != null){
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}
}

获致手机信息工具类:

public class GetPhoneInfoUtil {
	// 获取内核版本,gcc版本等
	public String fetch_version_info(){
		String result = null;
		CMDExecutor cmdexe = new CMDExecutor();
		String[] args = {"/system/bin/cat", "/proc/version"}; // "/system/bin/cat" 指令绝对路径
		result = cmdexe.run(args, "system/bin/");   // 已指定指令执行目录,上面绝对路径可写相对路径 "cat"
		return result;
	}
	
	// 获取CPU信息
	public String fetch_cpu_info() {
		String result = null;
		CMDExecutor cmdexe = new CMDExecutor();
		String[] args = { "/system/bin/cat", "/proc/cpuinfo" };
		result = cmdexe.run(args, "/system/bin/");
		return result;
	}
	
	// 要加输权限:android.permission.READ_PHONE_STATE
	// 运营商信息
	public String fetch_tel_status(Context context) {
		String result = null;
		TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
		String str = " ";
		str += "DeviceId(IMEI) = " + tm.getDeviceId() + "\n";
		str += "DeviceSoftwareVersion = " + tm.getDeviceSoftwareVersion() + "\n";
		int mcc = context.getResources().getConfiguration().mcc;
		int mnc = context.getResources().getConfiguration().mnc;
		str += "IMSI MCC (Mobile Country Code): " + String.valueOf(mcc) + "\n";
		str += "IMSI MNC (Mobile Network Code): " + String.valueOf(mnc) + "\n";
		result = str;
		return result;
	}
	
	
	private static StringBuffer buffer = null;
	public static String initProperty(String description, String propertyStr) {
		if (buffer == null) {
			buffer = new StringBuffer();
		}
		buffer.append(description).append(":\t");
		buffer.append(System.getProperty(propertyStr)).append("\n");
		return buffer.toString();
	}
	public static String getSystemProperty() {
		buffer = new StringBuffer();
		initProperty("java.vendor.url", "java.vendor.url");
		initProperty("java.class.path", "java.class.path");
		initProperty("os.name", "os.name");
		initProperty("os.version", "os.version");
		initProperty("user.home", "user.home");
		return buffer.toString();
	}
	
	// 获取网络信息
	public static String fetch_netcfg_info(){
		String result = null;
		CMDExecutor cmdexe = new CMDExecutor();
		String[] args = { "/system/bin/netcfg" };
		result = cmdexe.run(args, "/system/bin/");
		return result;
	}
	
	// 磁盘信息
	public static String fetch_disk_info() {
		String result = null;
		CMDExecutor cmdexe = new CMDExecutor();
		String[] args = { "/system/bin/df" };
		result = cmdexe.run(args, "/system/bin/");

		return result;
	}
	
	// 获取显示频信息
	public static String getDisplayMetrics(Context context) {
		String str = "";
		DisplayMetrics dm = new DisplayMetrics();
		dm = context.getResources().getDisplayMetrics();
		// 上面一行等同于下面两行
		//WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
		//wm.getDefaultDisplay().getMetrics(dm);
		int screenWidth = dm.widthPixels;
		int screenHeight = dm.heightPixels;
		float density = dm.density;
		float xdpi = dm.xdpi;
		float ydpi = dm.ydpi;
		str += "The absolute width: " + String.valueOf(screenWidth) + "pixels \n";
		str += "The absolute heightin: " + String.valueOf(screenHeight)
				+ "pixels  \n";
		str += "The logical density of the display :"
				+ String.valueOf(density) + " \n";
		str += "X dimension : " + String.valueOf(xdpi) + "pixels per inch \n";
		str += "Y dimension : " + String.valueOf(ydpi) + "pixels per inch \n";
		return str;
	}
}

关于System.getProperty(),可参考

System.getProperty()方法可以获取的值



android获取手机信息

标签:android   linux指令解析器   手机信息   

原文地址:http://blog.csdn.net/cy524563/article/details/41682765

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!