集合几个工具方法,方便以后使用。
1.获取手机 分辨率屏幕:
public static void printScreenInfor(Context context){ DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics(); int width = displayMetrics.widthPixels; int height = displayMetrics.heightPixels; float density = displayMetrics.density; float scaledDensity = displayMetrics.scaledDensity; Log.d(null, String.format("screen info: width = %d, height = %d, density = %f , scaledDensity = %f ", width, height, density, scaledDensity)); }
2.获取手机 密度
1 public static double getDensity(Activity context) { 2 DisplayMetrics displaysMetrics = new DisplayMetrics(); 3 if(context == null ){ 4 LogHelper.e(LogHelper.APPUTIL, "传入的应为activity"); 5 return 0; 6 } 7 context.getWindowManager().getDefaultDisplay().getMetrics(displaysMetrics); 8 float density = displaysMetrics.scaledDensity; 9 10 return density; 11 }
3.获取手机的dp和px转化:
public static int dip2px(Context context, float dipValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dipValue * scale + 0.5f); } public static int px2dip(Context context, float pxValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (pxValue / scale + 0.5f); }
4.获取手机真实的物理尺寸
public static double getScreenPhysicalSize(Activity activity) { DisplayMetrics dm = new DisplayMetrics(); activity.getWindowManager().getDefaultDisplay().getMetrics(dm); double diagonalPixels = Math.sqrt(Math.pow(dm.widthPixels, 2) + Math.pow(dm.heightPixels, 2)); return diagonalPixels / (160 * dm.density); }
5.判断手机是否是平板
1 public static boolean isTablet(Context context) { 2 return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE; 3 }
6.获取手机的编译版本 制造厂商等
1 String osVersion = android.os.Build.VERSION.SDK;//编译版本 2 String model = Build.MODEL;//手机品牌 3 String manufacture = Build.manufacture;// 4 String cpu = Build.cpu_ABI;
7.获取手机mac地址等
1 public String getLocalMacAddress() { 2 WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); 3 WifiInfo info = wifi.getConnectionInfo(); 4 return info.getMacAddress(); 5 }
8.判断是有网络
1 public static boolean isNetworkAvailable(Context context) { 2 boolean flag = false; 3 if (context != null) { 4 ConnectivityManager connMgr = (ConnectivityManager) context 5 .getSystemService(Context.CONNECTIVITY_SERVICE); 6 NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); 7 if (networkInfo != null && networkInfo.isConnected()) { 8 flag = true; 9 } 10 } 11 return flag; 12 }
9.其他待补充