Android中擷取裝置的各種資訊總結_Android

來源:互聯網
上載者:User

一、螢幕解析度

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具有一定參考借鑒價值,希望對大家能有所協助,如果有疑問大家可以留言交流。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.