BatteryManager and its properties in Android?

In this article, we are going to learn about BatteryManager and how we can use it to know battery status and other battery properties?
Submitted by Manu Jemini, on January 30, 2018

First of all, we need to learn the method to use BatteryManager and check the current status of battery. In the example, below we are going to use the os.BatteryManager class to have all the functionality required for BatteryManager.

To implement this in your program you will need to import: android.os.BatteryManager in your java file. Now on your XML File we need a button for test the battery status. For this just add button like this:

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click and check the battery Status"
        android:id="@+id/button"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="150dp"
        android:onClick="check"/>

Now, this button have a onClick attribute which is referring to a function called check, this function is implemented in JAVA file with corresponding logics to check the current battery status and use the Toast class to make a Text.

Now in the java class we will get the status of the battery charging from the line below: BatteryManager.BATTERY_PLUGGED_AC;

This will return a Boolean which we will check and if it’s true we will know that this is the current status.

We will check if the battery is full or not if the battery is not full we will show the battery level in percentage. To check if the battery is full we will use this line: status == BatteryManager.BATTERY_STATUS_FULL;

Toast.makeText(getApplicationContext(),
    "Device is "+isPercentage+"% charge",Toast.LENGTH_LONG).show();

1) Java file:

package com.example.hp.myapplication;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {
    Button b;
    int isPercentage;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b=(Button)findViewById(R.id.button);

    }
    public void check(View v){
        IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        Intent batteryStatus = registerReceiver(null, ifilter);

        int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
        BatteryManager bm=(BatteryManager)getSystemService(BATTERY_SERVICE);
        isPercentage = bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);
        boolean isFull = status == BatteryManager.BATTERY_STATUS_FULL;


       if(isFull)
        {
            Toast.makeText(getApplicationContext(),"Device is fully charged",Toast.LENGTH_LONG).show();
        }
        else {
            Toast.makeText(getApplicationContext(),"Device is "+isPercentage+"% charge",Toast.LENGTH_LONG).show();
        }
    }

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

}

2) XML file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.hp.myapplication.MainActivity">

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click and check the battery Status"
        android:id="@+id/button"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="150dp"
        android:onClick="check"/>

</RelativeLayout>

3) Menifest file:

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

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <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>

Output

battery manager android output 1

battery manager android output 2

Related Tutorials



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.