安卓开发获取设备相关信息

相信各位Android的开发者们都知道,现在几乎所有的app都需要获得设备信息,那么下面这篇文章就来详细说说获取设备信息的方法。

一、屏幕分辨率

下面展示一些 内联代码片

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;

或者

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int width = metrics.widthPixels;
int height = metrics.heightPixels

上面的代码是要在能获取到Activity的情况下使用的,如果无法获取到Activity,则可以使用一下的代码:

WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point point = new Point();
display.getSize(point);
int width = point.x;
int height = point.y;

二、屏幕尺寸

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width=dm.widthPixels;
int height=dm.heightPixels;
int dens=dm.densityDpi;
double wi=(double)width/(double)dens;
double hi=(double)height/(double)dens;
double x = Math.pow(wi,2);
double y = Math.pow(hi,2);
double screenInches = Math.sqrt(x+y);

同样,上面的代码需要在能获取到Activity。

三、获取app名称

public static String getAppName(Context context) {
  String appName = "";
  try {
    PackageManager packageManager = context.getPackageManager();
    ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
    appName = (String) packageManager.getApplicationLabel(applicationInfo);
  } catch (PackageManager.NameNotFoundException e) {
    e.printStackTrace();
  }
  return appName;
}

四、获取设备厂商和设备名称信息

// 设备厂商
String brand = Build.BRAND;
// 设备名称
String model = Build.MODEL;

获取DeviceID,SIM和IMSI

TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String deviceId = tm.getDeviceId();
String sim = tm.getSimSerialNumber();
String imsi = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE).getSubscriberId()

注意需要在AndroidManifest中添加权限

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

五、获取网络状态

public static String getAPNType(Context context) {
  //结果返回值
  String netType = "nono_connect";
  //获取手机所有连接管理对象
  ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  //获取NetworkInfo对象
  NetworkInfo networkInfo = manager.getActiveNetworkInfo();
  //NetworkInfo对象为空 则代表没有网络
  if (networkInfo == null) {
    return netType;
  }
  //否则 NetworkInfo对象不为空 则获取该networkInfo的类型
  int nType = networkInfo.getType();
  if (nType == ConnectivityManager.TYPE_WIFI) {
    //WIFI
    netType = "wifi";
  } else if (nType == ConnectivityManager.TYPE_MOBILE) {
    int nSubType = networkInfo.getSubtype();
    TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    //4G
    if (nSubType == TelephonyManager.NETWORK_TYPE_LTE
        && !telephonyManager.isNetworkRoaming()) {
      netType = "4G";
    } else if (nSubType == TelephonyManager.NETWORK_TYPE_UMTS || nSubType == TelephonyManager.NETWORK_TYPE_HSDPA || nSubType == TelephonyManager.NETWORK_TYPE_EVDO_0 && !telephonyManager.isNetworkRoaming()) {
      netType = "3G";
    //2G 移动和联通的2G为GPRS或EGDE,电信的2G为CDMA
    } else if (nSubType == TelephonyManager.NETWORK_TYPE_GPRS || nSubType == TelephonyManager.NETWORK_TYPE_EDGE || nSubType == TelephonyManager.NETWORK_TYPE_CDMA && !telephonyManager.isNetworkRoaming()) {
      netType = "2G";
    } else {
      netType = "2G";
    }
  }
  return netType;
}

六、判断设备是否root

网上有很多判断方法,但有些会在界面上弹窗提示获取权限,下面介绍一种无需弹窗判断设备是否root的方法:

/** 判断手机是否root,不弹出root请求框<br/> */
  public static boolean isRoot() {
    String binPath = "/system/bin/su";
    String xBinPath = "/system/xbin/su";
    if (new File(binPath).exists() && isExecutable(binPath))
      return true;
    if (new File(xBinPath).exists() && isExecutable(xBinPath))
      return true;
    return false;
  }

  private static boolean isExecutable(String filePath) {
    Process p = null;
    try {
      p = Runtime.getRuntime().exec("ls -l " + filePath);
      // 获取返回内容
      BufferedReader in = new BufferedReader(new InputStreamReader(
          p.getInputStream()));
      String str = in.readLine();
      if (str != null && str.length() >= 4) {
        char flag = str.charAt(3);
        if (flag == 's' || flag == 'x')
          return true;
      }
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (p != null) {
        p.destroy();
      }
    }
    return false;
  }

以上就是关于获取Android中设备各种信息的全部内容,这篇文章对大家开发Android App具有一定参考借鉴价值,希望对大家能有所帮助,如果有疑问大家可以留言交流。

转载自: https://www.jb51.net/article/92100.htm.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值