Android Tool Class for Obtaining Mobile Phone Information

  • 2021-10-15 11:33:11
  • OfStack

Online collection of 1 access to the collection of information code, made into a tool class, can be easily called later.


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.format.Formatter;

/**
 *  Tool class for obtaining mobile phone information 
 * 
 */
public class PhoneUtil {

  private static PhoneUtil instance;

  private TelephonyManager tm;
  private Activity act;

  private PhoneUtil(Activity act) {
    tm = (TelephonyManager) act.getSystemService(Context.TELEPHONY_SERVICE);
    this.act = act;
  }

  public static PhoneUtil getInstance(Activity act) {
    if (instance == null) {
      instance = new PhoneUtil(act);
    } else if (instance.act != act) {
      instance = new PhoneUtil(act);
    }
    return instance;
  }

  /**  Is it in flight mode  */
  public boolean isAirModeOpen() {
    return (Settings.System.getInt(act.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0) == 1 ? true
        : false);
  }

  /**  Get the mobile phone number  */
  public String getPhoneNumber() {
    return tm == null ? null : tm.getLine1Number();
  }

  /**  Get the network type (temporarily unavailable)  */
  public int getNetWorkType() {
    return tm == null ? 0 : tm.getNetworkType();
  }

  /**  Get a mobile phone sim Serial number of the card ( IMSI )  */
  public String getIMSI() {
    return tm == null ? null : tm.getSubscriberId();
  }

  /**  Get a mobile phone IMEI */
  public String getIMEI() {
    return tm == null ? null : tm.getDeviceId();
  }

  /**  Get the mobile phone model  */
  public static String getModel() {
    return android.os.Build.MODEL;
  }

  /**  Get the brand of mobile phone  */
  public static String getBrand() {
    return android.os.Build.BRAND;
  }

  /**  Get the mobile phone system version  */
  public static String getVersion() {
    return android.os.Build.VERSION.RELEASE;
  }

  /**  Get the total memory of the mobile phone system  */
  public String getTotalMemory() {
    String str1 = "/proc/meminfo";//  System memory information file 
    String str2;
    String[] arrayOfString;
    long initial_memory = 0;

    try {
      FileReader localFileReader = new FileReader(str1);
      BufferedReader localBufferedReader = new BufferedReader(localFileReader, 8192);
      str2 = localBufferedReader.readLine();//  Read meminfo No. 1 1 Row, total system memory size 

      arrayOfString = str2.split("\\s+");

      initial_memory = Integer.valueOf(arrayOfString[1]).intValue() * 1024;//  Get the total system memory in KB , multiplied by 1024 Convert to Byte
      localBufferedReader.close();

    } catch (IOException e) {
    }
    return Formatter.formatFileSize(act, initial_memory);// Byte Convert to KB Or MB Memory size normalization 
  }

  /**  Get the screen width of the mobile phone  */
  public int getScreenWidth() {
    return act.getWindowManager().getDefaultDisplay().getWidth();
  }

  /**  Get the height and width of the mobile phone screen  */
  public int getScreenHeight() {
    return act.getWindowManager().getDefaultDisplay().getHeight();
  }

  /**  Get the application package name  */
  public String getPackageName() {
    return act.getPackageName();
  }

  /**
   *  Get a mobile phone MAC Address   Only the mobile phone is turned on wifi To get it mac Address 
   */
  public String getMacAddress() {
    String result = "";
    WifiManager wifiManager = (WifiManager) act.getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    result = wifiInfo.getMacAddress();
    return result;
  }

  /**
   *  Get a mobile phone CPU Information  //1-cpu Model  //2-cpu Frequency 
   */
  public String[] getCpuInfo() {
    String str1 = "/proc/cpuinfo";
    String str2 = "";
    String[] cpuInfo = { "", "" }; // 1-cpu Model  //2-cpu Frequency 
    String[] arrayOfString;
    try {
      FileReader fr = new FileReader(str1);
      BufferedReader localBufferedReader = new BufferedReader(fr, 8192);
      str2 = localBufferedReader.readLine();
      arrayOfString = str2.split("\\s+");
      for (int i = 2; i < arrayOfString.length; i++) {
        cpuInfo[0] = cpuInfo[0] + arrayOfString[i] + " ";
      }
      str2 = localBufferedReader.readLine();
      arrayOfString = str2.split("\\s+");
      cpuInfo[1] += arrayOfString[2];
      localBufferedReader.close();
    } catch (IOException e) {
    }
    return cpuInfo;
  }

  /**  Get Application In meta-data Content  */
  public String getMetaData(String name) {
    String result = "";
    try {
      ApplicationInfo appInfo = act.getPackageManager().getApplicationInfo(getPackageName(),
          PackageManager.GET_META_DATA);
      result = appInfo.metaData.getString(name);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return result;
  }

}


Related articles: