Today we are going to discuss about how to monitoring battery charging level in android.we get battery charging level by using BatteryManager and Sticky Intent . it is very easy
BatteryManager: It gives total information regarding battery
Intent.ACTION_BATTERY_CHANGED :is one of Sticky Intent
Create a Project
add the below permission in your AndroidManifest.xml file
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ypp.battery"
android:versionCode="4"
android:versionName="1.3" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
>
<activity
android:name="com.ypp.battery.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
then create java file and add the below code in your java file
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = registerReceiver(null, ifilter);
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
float batteryPct = ((float)level / (float)scale) * 100.0f;
int a=(int) batteryPct;
batterypercentage.setText(String.valueOf(a)+"%");
Note: batterypercentage is TextView
Finally,Run your application
OUTPUT:
BatteryManager: It gives total information regarding battery
Intent.ACTION_BATTERY_CHANGED :is one of Sticky Intent
Create a Project
add the below permission in your AndroidManifest.xml file
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ypp.battery"
android:versionCode="4"
android:versionName="1.3" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
>
<activity
android:name="com.ypp.battery.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
then create java file and add the below code in your java file
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = registerReceiver(null, ifilter);
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
float batteryPct = ((float)level / (float)scale) * 100.0f;
int a=(int) batteryPct;
batterypercentage.setText(String.valueOf(a)+"%");
Note: batterypercentage is TextView
Finally,Run your application
OUTPUT: