Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android獲取手機信息

android獲取手機信息

編輯:關於Android編程

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()方法可以獲取的值



  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved