SoFunction
Updated on 2025-04-09

A summary of various information about devices in Android

1. Screen resolution

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

or:

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

The above code is to be obtainedActivityIf it cannot be obtainedActivity, you can use the following code:

WindowManager wm = (WindowManager)(Context.WINDOW_SERVICE);
Display display = ();
Point point = new Point();
(point);
int width = ;
int height = ;

2. Screen size

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

Similarly, the above code needs to be able to obtain the Activity.

3. Get the app name

public static String getAppName(Context context) {
  String appName = "";
  try {
    PackageManager packageManager = ();
    ApplicationInfo applicationInfo = ((), 0);
    appName = (String) (applicationInfo);
  } catch ( e) {
    ();
  }
  return appName;
}

4. Obtain information on equipment manufacturer and equipment name

// Equipment manufacturerString brand = ;
// Device nameString model = ;

Get DeviceID, SIM and IMSI

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

Note that it needs to beAndroidManifestAdd permissions

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

5. Obtain network status

public static String getAPNType(Context context) {
  //Result return value  String netType = "nono_connect";
  //Get all connection management objects of the mobile phone  ConnectivityManager manager = (ConnectivityManager) (Context.CONNECTIVITY_SERVICE);
  //Get NetworkInfo object  NetworkInfo networkInfo = ();
  //NetworkInfo object is empty means there is no network  if (networkInfo == null) {
    return netType;
  }
  // Otherwise, the NetworkInfo object is not empty, then the type of the networkInfo is obtained.  int nType = ();
  if (nType == ConnectivityManager.TYPE_WIFI) {
    //WIFI
    netType = "wifi";
  } else if (nType == ConnectivityManager.TYPE_MOBILE) {
    int nSubType = ();
    TelephonyManager telephonyManager = (TelephonyManager) (Context.TELEPHONY_SERVICE);
    //4G
    if (nSubType == TelephonyManager.NETWORK_TYPE_LTE
        &amp;&amp; !()) {
      netType = "4G";
    } else if (nSubType == TelephonyManager.NETWORK_TYPE_UMTS || nSubType == TelephonyManager.NETWORK_TYPE_HSDPA || nSubType == TelephonyManager.NETWORK_TYPE_EVDO_0 &amp;&amp; !()) {
      netType = "3G";
    //2G Mobile and China Unicom's 2G is GPRS or EGDE, and Telecom's 2G is CDMA.    } else if (nSubType == TelephonyManager.NETWORK_TYPE_GPRS || nSubType == TelephonyManager.NETWORK_TYPE_EDGE || nSubType == TelephonyManager.NETWORK_TYPE_CDMA &amp;&amp; !()) {
      netType = "2G";
    } else {
      netType = "2G";
    }
  }
  return netType;
}

6. Determine whether the device is rooted

There are many judgment methods on the Internet, but some will pop up windows on the interface to prompt you to obtain permissions. The following is a method to determine whether the device is rooted without popup windows:

/** Determine whether the phone is rooted, and the root request box does not pop up<br/> */
  public static boolean isRoot() {
    String binPath = "/system/bin/su";
    String xBinPath = "/system/xbin/su";
    if (new File(binPath).exists() &amp;&amp; isExecutable(binPath))
      return true;
    if (new File(xBinPath).exists() &amp;&amp; isExecutable(xBinPath))
      return true;
    return false;
  }

  private static boolean isExecutable(String filePath) {
    Process p = null;
    try {
      p = ().exec("ls -l " + filePath);
      // Get the return content      BufferedReader in = new BufferedReader(new InputStreamReader(
          ()));
      String str = ();
      if (str != null &amp;&amp; () &gt;= 4) {
        char flag = (3);
        if (flag == 's' || flag == 'x')
          return true;
      }
    } catch (IOException e) {
      ();
    } finally {
      if (p != null) {
        ();
      }
    }
    return false;
  }

7. Summary

The above is all about obtaining various information about devices in Android. This article has certain reference value for everyone to develop Android Apps. I hope it will be helpful to everyone. If you have any questions, you can leave a message to communicate.