How to Create app to detect android mobile phone battery health dynamically and show health status on screen.
Android mobile phone’s battery contain higher battery voltage and battery backup. So with the BatteryManager class android app developer can easily detect the status of battery health dynamically. So here is the complete step by step tutorial for Check/Get battery health in android programmatically.
How to Check/Get battery health in android programmatically.
Code for MainActivity.java file.
package com.getbatteryhealth_android_examples.com; 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 status; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textview = (TextView)findViewById(R.id.textView1); button = (Button)findViewById(R.id.button1); 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); } }); } BroadcastReceiver broadcastreceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { status = intent.getIntExtra(BatteryManager.EXTRA_HEALTH,0); if(status == BatteryManager.BATTERY_HEALTH_COLD){ textview.setText("Battery health = Cold"); } if (status == BatteryManager.BATTERY_HEALTH_DEAD){ textview.setText("Battery health = Dead"); } if (status == BatteryManager.BATTERY_HEALTH_GOOD){ textview.setText("Battery health = Good"); } if (status == BatteryManager.BATTERY_HEALTH_OVERHEAT){ textview.setText("Battery health = Over Heat"); } if (status == BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE){ textview.setText("Battery health = Over Voltage"); } if(status == BatteryManager.BATTERY_HEALTH_UNKNOWN){ textview.setText("Battery health = Unknown"); } if(status == BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE){ textview.setText("Battery health = Unspecified failure"); } } }; }
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.getbatteryhealth_android_examples.com.MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="184dp" android:text="Battery health" 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="17dp" android:text="Click here to Check/Get battery health in android programmatically" /> </RelativeLayout>
Screenshot: