In this tutorial, we will learn how to create an app that displays battery information in an Android environment using Java.
Battery usage is a very important factor in modern smartphones, and users want to check their battery status in real-time.
This app will allow users to check battery status, charging status, battery level, and more.
1. Creating a Project
Open Android Studio and create a new project. Select the “Empty Activity” template and set up the project name, package name,
storage location, etc., and then click “Finish”. A basic Android project has been created.
2. Modifying AndroidManifest.xml
You need to add the necessary permissions to the manifest file to access battery information.
Open the project’s AndroidManifest.xml
file and modify it as follows.
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.batteryinfo"> <uses-permission android:name="android.permission.BATTERY_STATS"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.AppCompat.Light.NoActionBar"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application> </manifest>
3. Creating Layout File
Modify the activity_main.xml
file to define the app’s UI.
Add TextViews to display battery information to the user.
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/batteryLevel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Battery Level: 100%" android:textSize="24sp" android:layout_centerInParent="true"/> <TextView android:id="@+id/batteryStatus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Charging Status: Charging" android:textSize="18sp" android:layout_below="@id/batteryLevel" android:layout_centerHorizontal="true"/> </RelativeLayout>
4. Modifying MainActivity.java
Now it’s time to implement the logic to obtain battery information. Open the MainActivity.java
file and
add the code to get battery status and level.
package com.example.batteryinfo; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.BatteryManager; import android.os.Bundle; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { private TextView batteryLevelTextView; private TextView batteryStatusTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); batteryLevelTextView = findViewById(R.id.batteryLevel); batteryStatusTextView = findViewById(R.id.batteryStatus); registerBatteryReceiver(); } private void registerBatteryReceiver() { IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); registerReceiver(batteryReceiver, filter); } private final BroadcastReceiver batteryReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); int batteryPct = (int) ((level / (float) scale) * 100); int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1); String statusString; switch (status) { case BatteryManager.BATTERY_STATUS_CHARGING: statusString = "Charging"; break; case BatteryManager.BATTERY_STATUS_DISCHARGING: statusString = "Discharging"; break; case BatteryManager.BATTERY_STATUS_FULL: statusString = "Fully Charged"; break; default: statusString = "Unknown"; break; } batteryLevelTextView.setText("Battery Level: " + batteryPct + "%"); batteryStatusTextView.setText("Charging Status: " + statusString); } }; @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(batteryReceiver); } }
5. Running the App
Now all the code is ready. Click the run button in Android Studio to run the app on an emulator or a real device.
When the app is running, battery level and status information will be displayed in real-time. This will be very important during the development process.
6. Implementing Additional Features
In addition to displaying basic battery information, we will implement a few additional features. For example,
we can add a battery overcharge prevention notification feature. This would send a notification to the user when the battery level reaches a certain level.
7. Implementing Overcharge Prevention Notification
// Code added to MainActivity.java private void checkBatteryLevel(int level) { if (level > 80) { showNotification("Battery Overcharge Warning", "Battery level has exceeded 80%."); } } private void showNotification(String title, String message) { // Implement notification related code }
8. Improving User Interface
Various design elements can be added to improve the app’s UI. For example, you can use ConstraintLayout to create more complex layouts,
or utilize Material Design elements to implement a more modern UI.
Conclusion
In this tutorial, we learned essential skills for Android app development using Java. Through the process of creating a battery information app,
we gained a deep understanding of the structure of Android apps, user interfaces, and data handling.
Building upon this foundation, you can add more features or explore other topics to enhance your Android app development skills.
Thank you! If you have any additional questions or feedback, please leave a comment.