Design an app in Android Studio to start an Intent service to monitor battery live. If the battery level is under 30%, send a notification to alert the user regarding the battery state. It’s made for Android 7. The app should show me my battery percentage. Use the following code as a basis:
BatteryManager bm = (BatteryManager)
getSystemService(BATTERY_SERVICE);
int percentage = bm.getIntProperty(
BatteryManager.BATTERY_PROPERTY_CAPACITY);
Assistant
To create an app in Android Studio that monitors battery life and sends a notification when the battery level is under 30%, you can follow these steps:
Create a new project in Android Studio.
In the project's AndroidManifest.xml file, add the necessary permissions to monitor battery status and display notifications. Add the following lines before the <application> tag:
Create a new Java class called BatteryService to implement the Intent service for monitoring battery level and sending notifications. Add the following code to the BatteryService.java file:
In the project's res folder, create a new folder called values. Inside the values folder, create a new XML file called strings.xml. Replace the existing code with the following:
Finally, update the MainActivity.java file to start the BatteryService using an Intent. Add the following code to the onCreate method:
java
Intent batteryIntent = new Intent(getApplicationContext(), BatteryService.class);
startService(batteryIntent);
That's it! Run the app on an Android 7 device or emulator, and you will be able to see the battery percentage on the main screen. If the battery level drops below 30%, a notification will be sent.