欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android中獲取設(shè)備的各種信息總結(jié)

 更新時(shí)間:2016年09月07日 09:23:11   投稿:daisy  
相信各位Android的開發(fā)者們都知道,現(xiàn)在幾乎所有的app都需要獲得設(shè)備信息,那么下面這篇文章就來詳細(xì)說說獲取設(shè)備信息的方法。

一、屏幕分辨率

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;
}

四、獲取設(shè)備廠商和設(shè)備名稱信息

// 設(shè)備廠商
String brand = Build.BRAND;
// 設(shè)備名稱
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中添加權(quán)限

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

五、獲取網(wǎng)絡(luò)狀態(tài)

public static String getAPNType(Context context) {
  //結(jié)果返回值
  String netType = "nono_connect";
  //獲取手機(jī)所有連接管理對(duì)象
  ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  //獲取NetworkInfo對(duì)象
  NetworkInfo networkInfo = manager.getActiveNetworkInfo();
  //NetworkInfo對(duì)象為空 則代表沒有網(wǎng)絡(luò)
  if (networkInfo == null) {
    return netType;
  }
  //否則 NetworkInfo對(duì)象不為空 則獲取該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 移動(dòng)和聯(lián)通的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;
}

六、判斷設(shè)備是否root

網(wǎng)上有很多判斷方法,但有些會(huì)在界面上彈窗提示獲取權(quán)限,下面介紹一種無需彈窗判斷設(shè)備是否root的方法:

/** 判斷手機(jī)是否root,不彈出root請(qǐng)求框<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);
      // 獲取返回內(nèi)容
      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;
  }

七、總結(jié)

以上就是關(guān)于獲取Android中設(shè)備各種信息的全部內(nèi)容,這篇文章對(duì)大家開發(fā)Android App具有一定參考借鑒價(jià)值,希望對(duì)大家能有所幫助,如果有疑問大家可以留言交流。

相關(guān)文章

最新評(píng)論