Android detect battery charging state

In this Android tip, I am going to show you how to detect the battery charging state of an Android device. For example, you might want to tell your user that now his/her device's battery is charging, discharging, or full. To accomplish the task, first you need to create an IntententFilter that matches the battery status change action.

IntentFilter filter=new IntentFilter(Intent.ACTION_BATTERY_CHANGED);

Then register the broadcast receiver with the current activity to receive a sticky intent sent from the Android system. A sticky intent stays around as the broadcast finished. You can use the null value for the receiver when you register the receiver to get the sticky intent. The intent sent by Android system when the battery charging state is changed is a sticky intent.

Intent intent=this.registerReceiver(null, filter);

Then Intent object sent from Android System (returned from the registerReceiver method) contains information about charging state, level, and other information about the battery. You can use constants defined in the BatteryManager class to access those information. For example, to get information about the charging state, you will pass the BatteryManager.EXTRA_STATUS value to the getIntExtra() method of the Intent. The getIntExtra() method returns a value indicating the charging state.

int status=intent.getIntExtra(BatteryManager.EXTRA_STATUS,-1);



Now to have a workable example application on battery charging state, you need to create a new Android project. Then open the MainActivity.java file (in scr folder). In the onCreate() method, write the code as shown below.

package com.example.androidexample;

import android.os.BatteryManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends Activity {

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//create an IntentFilter object that matches battery change action
IntentFilter filter=new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
//register broadcast receiver to receive a sticky intent
Intent intent=this.registerReceiver(null, filter);
//get battery status from the intent
int status=intent.getIntExtra(BatteryManager.EXTRA_STATUS,-1);
if(status==BatteryManager.BATTERY_STATUS_CHARGING)
Toast.makeText(this,"The battery is charging.", Toast.LENGTH_LONG).show();

else if (status==BatteryManager.BATTERY_STATUS_DISCHARGING)
Toast.makeText(this,"The battery is discharging.", Toast.LENGTH_LONG).show();

else if(status==BatteryManager.BATTERY_STATUS_FULL)
Toast.makeText(this,"The battery is full.", Toast.LENGTH_LONG).show();
}

public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

Android-detect-battery-charging-state

Posted by: Dara | post date: 09-06-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.