In this post, we are going to see how to get current battery voltage in android. You can use intent.getIntExtra(BatteryManager.EXTRA_HEALTH,0) to get information about current battery voltage.
Source code:
Step 1 :
Create an android application project named “GetBatteryHealthApp”.
Step 2 :
Change res ->layout -> activity_main.xml as below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?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:id="@+id/activity_main" 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.java2blog.getbatteryhealthapp.MainActivity"> <TextView android:id="@+id/textViewBatteryHealth" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="Current Battery Health" android:textAppearance="?android:attr/textAppearanceLarge" /> <Button android:id="@+id/buttonBatteryHealth" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textViewBatteryHealth" android:layout_centerHorizontal="true" android:layout_marginTop="16dp" android:text="Click here to Get battery Health" /> </RelativeLayout> |
Step 3:
Change src/main/packageName/MainActivity.java as below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
package com.java2blog.checkbatteryhealthapp; 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; import com.java2blog.checkbatteryhealthapp.R; public class MainActivity extends Activity { TextView textview; Button button; IntentFilter intentfilter; int deviceHealth; String currentBatteryHealth="Battery Health "; int batteryLevel; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button)findViewById(R.id.buttonBatteryHealth); textview = (TextView)findViewById(R.id.textViewBatteryHealth); intentfilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { MainActivity.this.registerReceiver(broadcastreceiver,intentfilter); } }); } private BroadcastReceiver broadcastreceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { deviceHealth = intent.getIntExtra(BatteryManager.EXTRA_HEALTH,0); if(deviceHealth == BatteryManager.BATTERY_HEALTH_COLD){ textview.setText(currentBatteryHealth+" = Cold"); } if(deviceHealth == BatteryManager.BATTERY_HEALTH_DEAD){ textview.setText(currentBatteryHealth+" = Dead"); } if (deviceHealth == BatteryManager.BATTERY_HEALTH_GOOD){ textview.setText(currentBatteryHealth+" = Good"); } if(deviceHealth == BatteryManager.BATTERY_HEALTH_OVERHEAT){ textview.setText(currentBatteryHealth+" = OverHeat"); } if (deviceHealth == BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE){ textview.setText(currentBatteryHealth+" = Over voltage"); } if (deviceHealth == BatteryManager.BATTERY_HEALTH_UNKNOWN){ textview.setText(currentBatteryHealth+" = Unknown"); } if (deviceHealth == BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE){ textview.setText(currentBatteryHealth+" = Unspecified Failure"); } } }; } |
As we want to get battery level health information, we need to register broadcast receiver and intent filter. An intent filter is used to tell native battery app that this app is listening for changes in native battery app.
Lets understand above code:
Intent.ACTION_BATTERY_CHANGED:
If you see the code we have used Intent.ACTION_BATTERY_CHANGED to get current battery info. It is sticky broadcast which contain battery charging state , level and other info. BatteryManager class contains all constant strings.
Sticky broadcast is broadcast which will remain active once registered.
1 2 3 |
MainActivity.this.registerReceiver(broadcastreceiver,intentfilter); |
Here we are registering created broadcastreceiver to the MainActivity and intent filter is listening for battery change events.
Run the app:
When you run above app on actual device, you will get below screen :