Get/Check battery voltage in android programmatically

How to access android device battery and show battery current voltage on screen using textview.

In this tutorial we are getting android mobile phone’s current voltage state with the use of intent.getIntExtra (BatteryManager. EXTRA_VOLTAGE,0) . This method will help us to detect the voltage of battery of our mobile phone. So here is the complete step by step tutorial for Get/Check battery voltage in android programmatically.

android-project-download-code-button

How to Get/Check battery voltage in android programmatically.

Code for MainActivity.java file.

 package com.batteryvoltage_android_examples.com;
import java.text.DecimalFormat;

import android.app.Activity;
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.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

 TextView textview;
 Button button;
 Context context;
 IntentFilter intentfilter;
 int volageValue;
 float fullVoltage;
 Double decimalVoltage;
 DecimalFormat decimalformat;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 button = (Button)findViewById(R.id.button1);
 textview = (TextView)findViewById(R.id.textView1);
 
 intentfilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
 
 button.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 MainActivity.this.registerReceiver(broadcastreceiver,intentfilter);
 
 }
 });
 }
 
 private BroadcastReceiver broadcastreceiver = new BroadcastReceiver() {
 @Override
 public void onReceive(Context context, Intent intent) {
 
 volageValue = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE,0);

 fullVoltage = (float) (volageValue * 0.001);

 decimalformat = new DecimalFormat("#.#");

 decimalVoltage = Double.valueOf(decimalformat.format(fullVoltage));

 textview.setText(decimalVoltage + "" + " Volt");

 }
 };
 
}

Code for activity_main.xml layout file.

 <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.batteryvoltage_android_examples.com.MainActivity" >

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="voltage"
 android:textAppearance="?android:attr/textAppearanceLarge" />

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/textView1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="27dp"
 android:text="Click here to Get battery voltage in android programmatically" />

</RelativeLayout>

Screenshot:

Get/Check battery voltage in android programmatically

Click here to download Get/Check battery voltage in android programmatically project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *