Android獲取手機版本號、品牌等 相關資訊工具類
阿新 • • 發佈:2018-11-14
主要有,獲取手機系統版本,獲取手機品牌、獲取軟體版本資訊、獲取螢幕尺寸寬高(包含和不包含虛擬鍵)以及獲取手機ip地址
public class DeviceUtils { /** * 品牌 */ public static String getDeviceBrand() { return android.os.Build.BRAND; } /** * 獲取作業系統版本號(如:2.3.3) */ public static String getDeviceVersionName() { return android.os.Build.VERSION.RELEASE; } /** * 獲取版本名稱 * * @param context * @return */ public static String getVersionName(Context context) { String versionName = null; try { //獲取包管理者 PackageManager pm = context.getPackageManager(); //獲取packageInfo PackageInfo info = pm.getPackageInfo(context.getPackageName(), 0); //獲取versionName versionName = info.versionName; } catch (PackageManager.NameNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return versionName; } /** * 獲取應用版本號 * * @param context * @return */ public static int getVersionCode(Context context) { int versionCode = 0; try { //獲取包管理者 PackageManager pm = context.getPackageManager(); //獲取packageInfo PackageInfo info = pm.getPackageInfo(context.getPackageName(), 0); //獲取versionCode versionCode = info.versionCode; } catch (PackageManager.NameNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return versionCode; } /** * @param context * @return 獲取螢幕原始尺寸高度,包括虛擬功能鍵高度 */ public static int getTotalHeight(Context context) { int dpi = 0; WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); Display display = windowManager.getDefaultDisplay(); DisplayMetrics displayMetrics = new DisplayMetrics(); @SuppressWarnings("rawtypes") Class c; try { c = Class.forName("android.view.Display"); @SuppressWarnings("unchecked") Method method = c.getMethod("getRealMetrics", DisplayMetrics.class); method.invoke(display, displayMetrics); dpi = displayMetrics.heightPixels; } catch (Exception e) { e.printStackTrace(); } return dpi; } /** * @param context * @return 獲取螢幕內容高度不包括虛擬按鍵 */ public static String getScreenHeight(Context context) { WindowManager wm = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE); DisplayMetrics outMetrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(outMetrics); int width = outMetrics.widthPixels; int height = outMetrics.heightPixels; return height + "," + width + ";"; } /** * ip地址 */ public static String getLocalIpAddress() { 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() && inetAddress instanceof Inet4Address) { return inetAddress.getHostAddress(); } } } } catch (SocketException ex) { ToastUtil.shortShow(ex.getMessage()); } return ""; } }