When altering the frequency of the background updates to scale back the effect of these updates on battery life, checking the present battery level and charging state is a superb place to start. The battery-life impact of performing application updates depends on the battery level and charging state of the device. The effect of performing updates while the device is charging over AC is negligible, so in most cases, maximize the refresh rate whenever the device is connected to a wall charger. Conversely, if the device is discharging, reducing the update rate helps prolong the battery life. Similarly, you’ll check the battery charge level, potentially reducing the frequency of—or even stopping—the updates when the battery charge is almost exhausted. There are 2 types of charging in any smartphone:
- USB Charging: Universal Serial Bus Charging: Charging from USB outlets such as USB port of a Laptop: 500 milliamps max.
- AC Charging: Alternating Current Charging: Charging from a wall charger: 2 amps max.
A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Kotlin language.
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.
Step 2: Working with the activity_main.xml file
Go to the activity_main.xml file, which represents the UI of the project. Add a Button, so whenever the user will click on the Button a Toast message with types of battery charging will be popped up on the screen. Below is the code for the activity_main.xml file.
Step 3: Working with the MainActivity.kt file
Finally, go to the MainActivity.kt file, and refer the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Output: Run on Physical Device
Keep an eye on the battery status in the status bar. The sequence of actions:
- Initially not connected.
- Connected to the USB port of the laptop.
- Connected to AC wall charger.
- How to Check if the Battery is Charging or Not in Android Programmatically?
- Current Internet Connection Type in Real-Time Programmatically in Android
- How to Check the Battery Level in Android Programmatically?
- Programmatically Check the Network Speed in Android
- How to programmatically hide Android soft keyboard
- How to Increase/Decrease Screen Brightness in Steps Programmatically in Android?
- How to Increase/Decrease Screen Brightness using Volume Keys Programmatically in Android?
- How to Detect Touch Event on Screen Programmatically in Android?
- How to Display the List of Sensors Present in an Android Device Programmatically?
- How to Invoke Keyboard Programmatically in Android?
- How to Find the Screen Resolution of a Device Programmatically in Android?
- How to Obtain the Connection Information Programmatically in Android?
- How to Programmatically Enable/Disable Bluetooth in Android?
- How to Change the Screen Orientation Programmatically using a Button in Android?
- How to Programmatically Enable/Disable Wi-Fi in Android?
- How to Adjust the Volume of Android Phone Programmatically from the App?
- How to Control Lottie Animations Programmatically in Android?
- How to Maximize/Minimize Screen Brightness Programmatically in Android?
- How to Listen for Volume Button and Back Key Events Programmatically in Android?
- How to Display Bluetooth Paired Devices Programmatically in Android?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.