分享一些获取手机基本信息和apk信息的函数

1.获取cpu信息 基本上耗时 几毫秒 0型号 1频率 ARMv7

  public static String[] getCpuInfo() {
        if (sCpuInfo == null) {
            String path = "/proc/cpuinfo";
            String data = "";
            String[] cpuInfo = { "", "" };
            String[] arrayOfString;
            try {
                FileReader file = new FileReader(path);
                BufferedReader localBufferedReader = new BufferedReader(file, 8192);
                data = localBufferedReader.readLine();
                arrayOfString = data.split("\\s+");
                for (int i = 2; i < arrayOfString.length; i++) {
                    cpuInfo[0] = cpuInfo[0] + arrayOfString[i] + " ";
                }
                data = localBufferedReader.readLine();
                arrayOfString = data.split("\\s+");
                cpuInfo[1] += arrayOfString[2];
                localBufferedReader.close();

                sCpuInfo = cpuInfo;
            } catch (IOException e) {
            }
        }
        return sCpuInfo;
    }

  public static String getCpuType() {
        String cpu = android.os.Build.CPU_ABI;
        if (TextUtils.isEmpty(cpu)) {
            String[] cpuInfo = getCpuInfo();
            if (cpuInfo != null && !TextUtils.isEmpty(cpuInfo[0])) {
                cpu = cpuInfo[0].toLowerCase();
            }
        } else {
            cpu = cpu.toLowerCase();
        }
        XLogger.d(TAG, "cpu:" + cpu);
        return cpu;
    }

2是否是特定类型,仅供参考


  public static boolean isArmCpu() {
        String cpu = getCpuType();
        if (TextUtils.isEmpty(cpu)) {
            return false;
        } else {
            return cpu.contains("arm");
        }
    }

    public static boolean isArmV7Cpu() {
        String cpu = getCpuType();
        if (TextUtils.isEmpty(cpu)) {
            return false;
        } else {
            return cpu.contains("v7");
        }
    }

3设备信息

  
    public static String getImsi() {
        TelephonyManager telephonyManager = (TelephonyManager) MApplication
				.mApplication.getSystemService(Context.TELEPHONY_SERVICE);
        String imsi = telephonyManager.getSubscriberId();
        return imsi;
    }

    public static String getImei() {
        TelephonyManager telephonyManager = (TelephonyManager) MApplication
				.mApplication.getSystemService(Context.TELEPHONY_SERVICE);
        String imei = telephonyManager.getDeviceId();
        return imei;
    }

    public static String getWifiMac() {
        WifiManager wifi = (WifiManager) MApplication
				.mApplication.getSystemService(Context.WIFI_SERVICE);
        WifiInfo mac = wifi.getConnectionInfo();
        String macAdress = (mac != null ? mac.getMacAddress() : "");
        return macAdress;
    }
    
    public static String getWifiBssid() {
        WifiManager wifi = (WifiManager) MApplication
				.mApplication.getSystemService(Context.WIFI_SERVICE);
        WifiInfo mac = wifi.getConnectionInfo();
        String bssidAdress = (mac != null ? mac.getBSSID() : "");
        return bssidAdress;
    }
    
    /**
     * 获取imsi前5位
     * 
     * @param context
     * @return
     */
    public static String getSimInfo(Context context) {
        String imsi = getImsi();
        String siminfo = "00000";
        if (!TextUtils.isEmpty(imsi) && imsi.length() > 4) {
            siminfo = imsi.substring(0, 5);
        }
        return siminfo;
    }
    
    /**
     * @return  返回当前手机的像素密度,xh 分辨率的是 320,h 的是 240 ,m 的是 160
     */
    public static int  getDefaultDisplayMetricsDensity(){
        if(mScreenDensity < 0){
            mScreenDensity = getDefaultDisplayMetrics().densityDpi;
        }
        return mScreenDensity;
    }
    
    /**
     * 获取手机屏幕宽,高<br>
     * 返回的 DisplayMetricx 不能被 copy ,因为该 DisplayMetricx 会随着手机的变化而变化,比如横竖屏切换时,DisplayMetricx 里面的宽、高的值就会发生对换
     * @return
     */
    public static DisplayMetrics getDefaultDisplayMetrics(){
        if(mDisplayMetrics == null){
            mDisplayMetrics = mApplication.getResources().getDisplayMetrics();
        }
    	return mDisplayMetrics;
    }
    
    public static int getScreenWidth(Activity activity) {
        DisplayMetrics dm = getDisplayMetrics(activity);
        if (dm == null) return 0;
        return dm.widthPixels;
    }

    public static int getScreenHeight(Activity activity) {
        DisplayMetrics dm = getDisplayMetrics(activity);
        if (dm == null) return 0;
        return dm.heightPixels;
    }

    private static DisplayMetrics getDisplayMetrics(Activity activity) {
    	if (activity == null) return null;
        DisplayMetrics dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        return dm;
    }
    
    public static float getDimensValue(int resId) {
		return MApplication.mApplication.getResources().getDimension(resId);
	}
    

4.dp、px、sp转换函数

	public static int convertDpToPx(int dp) {
		return (int) (getDefaultDisplayMetrics().density * dp + 0.5f);
	}

	public static float convertDpToPx(float dp) {
		return (getDefaultDisplayMetrics().density * dp);
	}

	public static int convertPxToDp(int px) {
		return (int) (px / getDefaultDisplayMetrics().density + 0.5f);
	}

	public static int convertSpToPx(float sp) {
		return (int) (getDefaultDisplayMetrics().scaledDensity * sp);
	}

5.获取手机系统版本,获取sdk版本

public static String getOSVersion() {
		return android.os.Build.VERSION.RELEASE;
	}

	public static int getSDKVersion() {
		return android.os.Build.VERSION.SDK_INT;
	}
    


6.根据ip获取本地mac等

   //根据IP获取本地Mac
	public static String getLocalMacAddressFromIp() {
		Reflection reflection = new Reflection();

		String mac_s = "";
		try {
			byte[] mac;
			NetworkInterface ne = NetworkInterface.getByInetAddress(InetAddress.getByName(getLocalIpAddress()));
			mac = (byte[]) reflection.invokeMethod(ne, "getHardwareAddress", new Object[] {});
			mac_s = byte2hex(mac);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mac_s;
	}
    
    public static String getLocalIpAddress() {  
	    try {  
	        String ipv4;  
	        List<NetworkInterface>  nilist = Collections.list(NetworkInterface.getNetworkInterfaces());  
	        for (NetworkInterface ni: nilist)   
	        {  
	            List<InetAddress>  ialist = Collections.list(ni.getInetAddresses());  
	            for (InetAddress address: ialist){  
	                if (!address.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ipv4=address.getHostAddress()))   
	                {   
	                    return ipv4;  
	                }  
	            }  
	        }  
	
	    } catch (SocketException ex) {  
	    }  
	    return null;  
	}
    public static String byte2hex(byte[] b) {
        StringBuffer hs = new StringBuffer(b.length);
        String stmp = "";
        int len = b.length;
        for (int n = 0; n < len; n++) {
            stmp = Integer.toHexString(b[n] & 0xFF);
            if (stmp.length() == 1)
                hs = hs.append("0").append(stmp);
            else {
                hs = hs.append(stmp);
            }
        }
        return String.valueOf(hs);
    }


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值