Detailed Explanation of Android System Tool Class

  • 2021-10-13 08:38:06
  • OfStack

In this paper, we share the specific code of Android system tool class for your reference. The specific contents are as follows

System tool class


public class systemUtil {

  // Hide ipad Bottom virtual key bar 
  @RequiresApi(api = Build.VERSION_CODES.KITKAT)
  public static void closeBottomBar(Activity activity){
    Window _window = activity.getWindow();
    WindowManager.LayoutParams params = _window.getAttributes();
    params.systemUiVisibility =
        View.SYSTEM_UI_FLAG_HIDE_NAVIGATION|View.SYSTEM_UI_FLAG_IMMERSIVE;
    _window.setAttributes(params);
  }

  // Soft keyboard does not pop up automatically 
  public static void softInputMode(Activity activity){
    activity.getWindow().setSoftInputMode(
        WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
        WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
  }

  // Keep the screen always bright 
  public static void screenLightUp(Activity activity){
    activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
  }

  // Get screen resolution 
  public static int[] defaultDisplay(Activity activity){
    int[] pixels = new int[2];
    DisplayMetrics dm = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
    pixels[0]=dm.widthPixels;
    pixels[1]=dm.heightPixels;
    return pixels;
  }

  // Get Android System version 
  public static String getSystemVersion() {
    return android.os.Build.VERSION.RELEASE;
  }

  // Get the equipment model 
  public static String getSystemModel() {
    return android.os.Build.MODEL;
  }

  // Get IMEI Identification number 
  // Required permissions  <uses-permission android:name="android.permission.READ_PHONE_STATE" />
  @SuppressLint("MissingPermission")
  public static String getIMEI(Activity activity) {
    //6.0 The above system dynamically adds permissions 
    if (ActivityCompat.checkSelfPermission(activity,
        Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
      ActivityCompat.requestPermissions(activity,
        new String[]{Manifest.permission.READ_PHONE_STATE},1);
    }
    TelephonyManager tm =
        (TelephonyManager) activity.getSystemService(Activity.TELEPHONY_SERVICE);
    return tm.getDeviceId();
  }

  // Get the current language of the system 
  public static String getSystemLanguage() {
    return Locale.getDefault().getLanguage();
  }

  // Obtain equipment power 
  @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  public static int getBattery(Context context){
    BatteryManager batteryManager =
        (BatteryManager)context.getSystemService(BATTERY_SERVICE);
    return batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);
  }

  // Get the available memory size of the device (GB)
  public static String getRomAvailableSize(Context context) {
    File path = Environment.getDataDirectory();
    StatFs stat = new StatFs(path.getPath());
    long blockSize = stat.getBlockSize();
    long availableBlocks = stat.getAvailableBlocks();
    return Formatter.formatFileSize(context, blockSize * availableBlocks);
  }

  // Gets the total amount of memory available on the device (GB)
  public static String getRomTotalSize(Context context) {
    File path = Environment.getDataDirectory();
    StatFs stat = new StatFs(path.getPath());
    long blockSize = stat.getBlockSize();
    long totalBlocks = stat.getBlockCount();
    return Formatter.formatFileSize(context, blockSize * totalBlocks);
  }

  // Obtain SD Total amount of cards available 
  public static String getSDTotalSize(Context context) {
    File path = Environment.getExternalStorageDirectory();
    StatFs stat = new StatFs(path.getPath());
    long blockSize = stat.getBlockSize();
    long totalBlocks = stat.getBlockCount();
    return Formatter.formatFileSize(context, blockSize * totalBlocks);
  }

  // Obtain sd Available size of card 
  private String getSDAvailableSize(Context context) {
    File path = Environment.getExternalStorageDirectory();
    StatFs stat = new StatFs(path.getPath());
    long blockSize = stat.getBlockSize();
    long availableBlocks = stat.getAvailableBlocks();
    return Formatter.formatFileSize(context, blockSize * availableBlocks);
   }

  // Restart the device 
  private void restartDevices() {
    String cmd = "su -c reboot";
    try {
      Runtime.getRuntime().exec(cmd);
    } catch (IOException e) {
      Log.i("restart"," Insufficient authority ");
    }
  }
}

System-related permissions


// Write to external storage 
android.permission.WRITE_EXTERNAL_STORAGE Allows writing to external storage 

// Read external storage 
android.permission.READ_EXTERNAL_STORAGE Allows reading of external storage 

// Read the system log 
android.permission.READ_LOGS Read the underlying logs of the system 

// Read the content of short message 
android.permission.READ_SMS Read the contents of the short message 

// Vibration 
android.permission.VIBRATE , permissible vibration 

// Restart the device 
android.permission.REBOOT Allow the program to restart the device 

// Install the application 
android.permission.INSTALL_PACKAGES Allow programs to install applications 

// Modify sound 
android.permission.MODIFY_AUDIO_SETTINGS Modify sound setting information 

// Recording 
android.permission.RECORD_AUDIO A microphone that records sound through a mobile phone or headset 

// Use a flash 
android.permission.FLASHLIGHT Allow access to the flash 

// Access the network 
android.permission.INTERNET Access a network connection, which may generate a GPRS Flow 

// Change wifi Status 
android.permission.CHANGE_WIFI_STATE , open wifi , change wifi Status 

// Get WiFi Status 
android.permission.ACCESS_WIFI_STATE Gets the current WiFi Access status and WLAN Hot spot information 

// Get network status 
android.permission.ACCESS_NETWORK_STATE Gets the status of network information, such as whether the current network connection is valid 

// Photographing authority 
android.permission.CAMERA Allow access to the camera to take pictures 

// Using Bluetooth 
android.permission.BLUETOOTH Allows programs to connect to paired Bluetooth devices 

// Battery status 
android.permission.BATTERY_STATS Permissions that allow applications to obtain battery status 

Related articles: