Native Mobile App Development With Java

Android battery low, battery percentage, battery status (charging or not), Battery Health BroadcastReceiver example

15-Sep-2024

Learn how to use BroadcastReceiver to detect battery status, percentage, and health in Android Studio Project Java

 Android battery low, battery percentage, battery status (charging or not) BroadcastReceiver example


1 / battery low BroadcastReceiver


 1.1 / Create A Class BatteryLowReceiver class and extends BroadcastReceiver,




package com.example.batterylowbroadcastreceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

public class BatteryLowReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action != null){
if(action.equals(Intent.ACTION_BATTERY_LOW)){
Log.d("BatteryLowReceiver", "Battery is low");
Toast.makeText(context, "Battery is low", Toast.LENGTH_SHORT).show();
}
}
}
}



1.2 / Active registerReceiver In MainActivity




package com.example.batterylowbroadcastreceiver;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
BatteryLowReceiver batteryLowReceiver = new BatteryLowReceiver();
IntentFilter intentFilter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

intentFilter = new IntentFilter(Intent.ACTION_BATTERY_LOW);
registerReceiver(batteryLowReceiver, intentFilter);
}

@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(batteryLowReceiver);
}
}



1.3 / Then Change AndroidManifest.xml (Add receiver in the application )



<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.BatteryLowBroadcastReceiver"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".BatteryLowReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BATTERY_LOW" />
</intent-filter>
</receiver>
</application>

</manifest>

 Now run the application. Toast will show when the device charge is low.




2 / battery percentage BroadcastReceiver 


 2.1 / Create A Class BatteryStatusReceiver class and extends BroadcastReceiver



package com.example.batterystatusbroadcastreceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.BatteryManager;
import android.widget.Toast;

public class BatteryStatusReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int batteryLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);

int batteryScale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

float batteryPercentageDecimal = batteryLevel / (float) batteryScale * 100;

int batteryPercentage = (int) batteryPercentageDecimal;

Toast.makeText(context, "Your phone battery level: " + batteryPercentage + "%", Toast.LENGTH_SHORT).show();
}
}



2.2 / Active registerReceiver In MainActivity




package com.example.batterystatusbroadcastreceiver;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
BatteryStatusReceiver batteryStatusReceiver = new BatteryStatusReceiver();
IntentFilter intentFilter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(batteryStatusReceiver, intentFilter);
}

@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(batteryStatusReceiver);
}
}

 

 2.3 / Then Change AndroidManifest.xml (Give Permissions and Add receiver in the application )






<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.BATTERY_STATS" />

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.BatteryStatusBroadcastReceiver"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".BatteryStatusReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BATTERY_CHANGED" />
</intent-filter>
</receiver>
</application>

</manifest>

 

Now run the application. Toast will show the percentage of battery level.



3 / battery status (charging or not)  BroadcastReceiver


 3.1 / Create A Class ChargerStatusReceiver class and extends BroadcastReceiver





package com.example.chargerconnectedornotbroadcastreceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.BatteryManager;
import android.util.Log;
import android.widget.Toast;

public class ChargerStatusReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {


int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = (status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL);

if (isCharging) {
Log.d("ChargerStatusReceiver", "Your phone is charging");
Toast.makeText(context, "Your phone is charging", Toast.LENGTH_SHORT).show();
} else {
Log.d("ChargerStatusReceiver", "Your phone is not charging");
Toast.makeText(context, "Your phone is not charging", Toast.LENGTH_SHORT).show();
}
}
}

 
 3.2 Active registerReceiver In MainActivity





package com.example.chargerconnectedornotbroadcastreceiver;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
ChargerStatusReceiver chargerStatusReceiver = new ChargerStatusReceiver();
IntentFilter intentFilter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(chargerStatusReceiver, intentFilter);
}

@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(chargerStatusReceiver);
}
}

 

3.3  / Then Change AndroidManifest.xml (Give Permissions and Add receiver in the application )



<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.BATTERY_STATS"/>

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.ChargerConnectedOrNotBroadcastReceiver"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<receiver android:name=".ChargerStatusReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BATTERY_CHANGED" />
</intent-filter>
</receiver>
</application>

</manifest>


  Now run the application and connect the phone to the charger. Toast will show whether your device is charging or not.




4 / battery health status  BroadcastReceiver


 4.1 / Create A Class BatteryHealthReceiver class and extends BroadcastReceiver





package com.example.batteryhealthbroadcastreceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.BatteryManager;
import android.util.Log;
import android.widget.Toast;

public class BatteryHealthReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();

if (action.equals(Intent.ACTION_BATTERY_CHANGED)){
int batterHealth = intent.getIntExtra(BatteryManager.EXTRA_HEALTH,BatteryManager.BATTERY_HEALTH_UNKNOWN);

if(batterHealth == BatteryManager.BATTERY_HEALTH_GOOD){
Log.d("BatteryHealthReceiver", "Battery health is good");
Toast.makeText(context, "Battery health is good", Toast.LENGTH_SHORT).show();
}
else if (batterHealth == BatteryManager.BATTERY_HEALTH_COLD){
Log.d("BatteryHealthReceiver", "Battery health is cold");
Toast.makeText(context, "Battery health is cold", Toast.LENGTH_SHORT).show();
}
else if (batterHealth == BatteryManager.BATTERY_HEALTH_DEAD){
Log.d("BatteryHealthReceiver", "Battery health is dead");
Toast.makeText(context, "Battery health is dead", Toast.LENGTH_SHORT).show();
}
else if (batterHealth == BatteryManager.BATTERY_HEALTH_OVERHEAT){
Log.d("BatteryHealthReceiver", "Battery health is overheat");
Toast.makeText(context, "Battery health is overheat", Toast.LENGTH_SHORT).show();
}
else if (batterHealth == BatteryManager.BATTERY_HEALTH_UNKNOWN){
Log.d("BatteryHealthReceiver", "Battery health is unknown");
Toast.makeText(context, "Battery health is unknown", Toast.LENGTH_SHORT).show();
}
else if (batterHealth == BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE){
Log.d("BatteryHealthReceiver", "Battery health is over voltage");
Toast.makeText(context, "Battery health is over voltage", Toast.LENGTH_SHORT).show();
}
else if (batterHealth == BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE){
Log.d("BatteryHealthReceiver", "Battery health is over unspecified");
Toast.makeText(context, "Battery health is over unspecified", Toast.LENGTH_SHORT).show();
}
}
}
}

 


4.2 / Active registerReceiver In MainActivity





package com.example.batteryhealthbroadcastreceiver;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

BatteryHealthReceiver batteryHealthReceiver = new BatteryHealthReceiver();
IntentFilter intentFilter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(batteryHealthReceiver, intentFilter);
}

@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(batteryHealthReceiver);
}
}


 

 4.3  /  Then Change AndroidManifest.xml (Give Permissions and Add receiver in the application )





<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.BATTERY_STATS" />

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.BatteryHealthBroadcastReceiver"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".BatteryHealthReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BATTERY_CHANGED" />
</intent-filter>
</receiver>
</application>

</manifest>


Now run the application. Toast will show the battery level.

Comments