In this tutorial, you will learn how to develop an Android application that displays both battery and device information. The tutorial will guide you through the process of designing a user interface using CardViews to organize and present the battery and device information in a visually appealing manner. You'll explore how to retrieve relevant data such as battery level, status (charging or discharging), device model, Android version, and other system information using Android APIs like BatteryManager and Build.
Submitted on July 07, 2024
Creating an Android app to display both battery and device information involves designing a layout with CardViews for each set of information and retrieving relevant data using Android APIs. Below is a structured guide on how to achieve this:
<uses-permission android:name="android.permission.BATTERY_STATS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
dependencies {
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
}
Create a layout file (activity_main.xml) that includes two CardViews for displaying battery and device information.
<?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">
<androidx.cardview.widget.CardView
android:id="@+id/cardBattery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:padding="16dp">
<!-- Battery information will be displayed here -->
<TextView
android:id="@+id/textBatteryInfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="@+id/cardDeviceInfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/cardBattery"
android:layout_margin="16dp"
android:padding="16dp">
<!-- Device information will be displayed here -->
<TextView
android:id="@+id/textDeviceInfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp" />
</androidx.cardview.widget.CardView>
</RelativeLayout>
Now, implement the logic in your ‘MainActivity.java’ to retrieve battery and device information and display it in the respective CardViews.
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Bundle;
import android.os.Environment;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
public class MainActivity extends AppCompatActivity {
private TextView textBatteryInfo;
private TextView textDeviceInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textBatteryInfo = findViewById(R.id.textBatteryInfo);
textDeviceInfo = findViewById(R.id.textDeviceInfo);
// Display battery information
displayBatteryInformation();
// Display device information
displayDeviceInfo();
}
private void displayBatteryInformation() {
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = this.registerReceiver(null, ifilter);
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
float batteryPct = level * 100 / (float) scale;
String status = "";
switch (batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1)) {
case BatteryManager.BATTERY_STATUS_CHARGING:
status = "Charging";
break;
case BatteryManager.BATTERY_STATUS_DISCHARGING:
status = "Discharging";
break;
case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
status = "Not Charging";
break;
case BatteryManager.BATTERY_STATUS_FULL:
status = "Full";
break;
case BatteryManager.BATTERY_STATUS_UNKNOWN:
status = "Unknown";
break;
}
String technology = batteryStatus.getStringExtra(BatteryManager.EXTRA_TECHNOLOGY);
int temperature = batteryStatus.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0);
int voltage = batteryStatus.getIntExtra(BatteryManager.EXTRA_VOLTAGE, 0);
String batteryInfo = "Battery Level: " + batteryPct + "%\n" +
"Status: " + status + "\n" +
"Technology: " + technology + "\n" +
"Temperature: " + (temperature / 10) + "°C\n" +
"Voltage: " + voltage + " mV";
textBatteryInfo.setText(batteryInfo);
}
private void displayDeviceInfo() {
String deviceName = android.os.Build.MODEL;
String storage = Environment.getDataDirectory().getAbsolutePath();
long totalMemory = Runtime.getRuntime().totalMemory();
long freeMemory = Runtime.getRuntime().freeMemory();
long usedMemory = totalMemory - freeMemory;
long maxMemory = Runtime.getRuntime().maxMemory();
String ram = usedMemory / (1024 * 1024) + " MB used / " + maxMemory / (1024 * 1024) + " MB max";
String display = getWindowManager().getDefaultDisplay().getName();
String temperature = "Unknown"; // You may add logic to get device temperature
String deviceInfo = "Device Name: " + deviceName + "\n" +
"Storage: " + storage + "\n" +
"RAM: " + ram + "\n" +
"Display: " + display + "\n" +
"Temperature: " + temperature;
textDeviceInfo.setText(deviceInfo);
}
}
Run your app on an Android device or emulator. You should see two CardViews displaying battery and device information based on the methods implemented in MainActivity.
This setup should help you create an Android app that displays battery and device information using CardViews effectively.
Implement the logic in MainActivity.kt to retrieve battery and device information:
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.BatteryManager
import android.os.Bundle
import android.os.Environment
import android.telephony.TelephonyManager
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.cardview.widget.CardView
class MainActivity : AppCompatActivity() {
private lateinit var textBatteryInfo: TextView
private lateinit var textDeviceInfo: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
textBatteryInfo = findViewById(R.id.textBatteryInfo)
textDeviceInfo = findViewById(R.id.textDeviceInfo)
// Display battery information
displayBatteryInformation()
// Display device information
displayDeviceInfo()
}
private fun displayBatteryInformation() {
val ifilter = IntentFilter(Intent.ACTION_BATTERY_CHANGED)
val batteryStatus: Intent? = this.registerReceiver(null, ifilter)
batteryStatus?.let {
val level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1)
val scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1)
val batteryPct = level * 100 / scale.toFloat()
val status = when (batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1)) {
BatteryManager.BATTERY_STATUS_CHARGING -> "Charging"
BatteryManager.BATTERY_STATUS_DISCHARGING -> "Discharging"
BatteryManager.BATTERY_STATUS_NOT_CHARGING -> "Not Charging"
BatteryManager.BATTERY_STATUS_FULL -> "Full"
else -> "Unknown"
}
val technology = batteryStatus.getStringExtra(BatteryManager.EXTRA_TECHNOLOGY)
val temperature = batteryStatus.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0) / 10
val voltage = batteryStatus.getIntExtra(BatteryManager.EXTRA_VOLTAGE, 0)
val batteryInfo = "Battery Level: $batteryPct%\n" +
"Status: $status\n" +
"Technology: $technology\n" +
"Temperature: $temperature°C\n" +
"Voltage: $voltage mV"
textBatteryInfo.text = batteryInfo
}
}
private fun displayDeviceInfo() {
val deviceName = android.os.Build.MODEL
val storage = Environment.getDataDirectory().absolutePath
val ram = (Runtime.getRuntime().totalMemory() / (1024 * 1024)).toString() + " MB"
val display = windowManager.defaultDisplay.name
val temperature = "Unknown" // You may add logic to get device temperature
val deviceInfo = "Device Name: $deviceName\n" +
"Storage: $storage\n" +
"RAM: $ram\n" +
"Display: $display\n" +
"Temperature: $temperature"
textDeviceInfo.text = deviceInfo
}
}
Key steps covered in the tutorial include:
By following this structured guide, you'll gain practical experience in accessing device information programmatically, organizing data within CardViews, and designing informative Android applications that provide essential device insights to users effectively.