集合几个工具方法,方便以后使用。

1.获取手机 分辨率屏幕:

  1. public static void printScreenInfor(Context context){
  2.  
  3. DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
  4.  
  5. int width = displayMetrics.widthPixels;
  6. int height = displayMetrics.heightPixels;
  7.  
  8. float density = displayMetrics.density;
  9. float scaledDensity = displayMetrics.scaledDensity;
  10.  
  11. Log.d(null, String.format("screen info: width = %d, height = %d, density = %f , scaledDensity = %f ", width, height, density, scaledDensity));
  12. }

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转化:

  1. public static int dip2px(Context context, float dipValue) {
  2. final float scale = context.getResources().getDisplayMetrics().density;
  3. return (int) (dipValue * scale + 0.5f);
  4. }
  5.  
  6. public static int px2dip(Context context, float pxValue) {
  7. final float scale = context.getResources().getDisplayMetrics().density;
  8. return (int) (pxValue / scale + 0.5f);
  9. }

4.获取手机真实的物理尺寸

  1. public static double getScreenPhysicalSize(Activity activity) {
  2. DisplayMetrics dm = new DisplayMetrics();
  3. activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
  4. double diagonalPixels = Math.sqrt(Math.pow(dm.widthPixels, 2) + Math.pow(dm.heightPixels, 2));
  5. return diagonalPixels / (160 * dm.density);
  6. }

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.其他待补充

android 几个工具方法的更多相关文章

  1. Android Studio更新升级方法

    自从2013 Google I/O大会之后,笔者就将android ide开发工具从eclipse迁移到Android Studio了,android studio一直在更新完善,为了与时俱进,我们当 ...

  2. Xamarin For Visual Studio 3.0.54.0 完整离线破解版(C# 开发Android、IOS工具 吾乐吧软件站分享)

    Xamarin For Visual Studio就是原本的Xamarin For Android 以及 Xamarin For iOS,最新版的已经把两个独立的插件合并为一个exe安装包了.为了区分 ...

  3. Xamarin Mono For Android 4.6.07004 完整离线安装破解版(C#开发Android、IOS工具)

      Xamarin是由Miguel de Icaza成立的一家新的独立公司,目的是给Mono一个继续奋斗的机会.Mono for Android (原名:MonoDroid)可以让开发人员使用 Mic ...

  4. Android开发常用工具类

    来源于http://www.open-open.com/lib/view/open1416535785398.html 主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java. 目前 ...

  5. Android Studio更新升级方法(转)

    自从2013 Google I/O大会之后,笔者就将android ide开发工具从eclipse迁移到Android Studio了,android studio一直在更新完善,为了与时俱进,我们当 ...

  6. Android各种获取Context方法

    首先讲一讲这四个函数的区别,后面还有我对context的一些理解区别如下所示: 原文链接http://stackoverflow.com/questions/6854265/getapplicatio ...

  7. Android 代码检查工具SonarQube

    http://blog.csdn.net/rain_butterfly/article/details/42170601 代码检查工具能帮我们检查一些隐藏的bug,代码检查工具中sonar是比较好的一 ...

  8. 正确使用Android性能分析工具——TraceView

    http://blog.jobbole.com/78995/     首页 最新文章 IT 职场 前端 后端 移动端 数据库 运维 其他技术 - 导航条 - 首页 最新文章 IT 职场 前端 - Ja ...

  9. Android中日志工具的使用

    添加LogCat到你的Eclipse日志在任何项目的开发过程中都会起到非常重要的作用,在Android项目中如果你想要查看日志则必须要使用LogCat工具.当你第一次在Eclipse中运行Androi ...

随机推荐

  1. BZOJ3143:[HNOI2013]游走(高斯消元)

    Description 一个无向连通图,顶点从1编号到N,边从1编号到M. 小Z在该图上进行随机游走,初始时小Z在1号顶点,每一步小Z以相等的概率随机选 择当前顶点的某条边,沿着这条边走到下一个顶点, ...

  2. AlexNet 分类 FashionMNIST

    from mxnet import gluon,init,nd,autograd from mxnet.gluon import data as gdata,nn from mxnet.gluon i ...

  3. [19/03/26-星期二] 容器_Map(图、键值对、映射)接口之HashMap(散列映射)&TreeMap(树映射)

    一.概念&方法 现实生活中,我们经常需要成对存储某些信息.比如,我们使用的微信,一个手机号只能对应一个微信账户,这就是一种成对存储的关系. Map就是用来存储“键(key)-值(value) ...

  4. XXE攻防——XML外部实体注入

    XXE攻防——XML外部实体注入 转自腾讯安全应急响应中心 一.XML基础知识 XML用于标记电子文件使其具有结构性的标记语言,可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进行定义的 ...

  5. P2213 [USACO14MAR]懒惰的牛The Lazy Cow_Sliver

    P2213 [USACO14MAR]懒惰的牛The Lazy Cow_Sliver 最大化一个子矩阵的和. 我们如何去做,dp和贪心呀! 大体题意:给定一个正方形,然后在正方形中求出一个大小已经给定的 ...

  6. 【Node.js】Jade视图模板的使用

    跟MVC里面的Rezor做差不多的事儿,但是比Rezor弱了一些,比较不喜欢CoffeeScript.Jade这种靠缩进来维系层级结构的做法,就好比接受不了c#中if下面只有一句很长的代码,但是却不加 ...

  7. Css animation 与 float 、flex 布局问题

    1. 有这样一段css html 代码 <div class="container"> <div class="float-left"> ...

  8. __doPostBack 方法解析

    function __doPostBack(eventTarget, eventArgument)的eventTarget参数是引起回送的控件的ID,eventArgument参数是回调参数(与控件相 ...

  9. CSS之元素

    CSSS书写位置 内嵌式 <head> <style type = "text/css"> **** </style> </head> ...

  10. DB数据源之SpringBoot+MyBatis踏坑过程(六)mysql中查看连接,配置连接数量

    DB数据源之SpringBoot+MyBatis踏坑过程(六)mysql中查看连接,配置连接数量 liuyuhang原创,未经允许禁止转载 系列目录连接 DB数据源之SpringBoot+Mybati ...