Android Tips and Tutorials to help you developing your android App. It includes source codes, examples and other helping stuff for you. You can find best Android, iPhone and Unity Source Codes at Andriod Help and Tips . Android Source Codes are of high quality and free. You can download Pc and Mobile Games, Graphics and other helping stuff from this blog. Android Tutorials and Examples can also be found on this blog. Unity Assets can be downloaded for free.
If you are developing an Android App and want to get Device Battery Information, then follow this tutorial. It will guide you how to get Battery Status, Battery Information and other details about battery in android.
The battery API is quite impressive, between other things not only can you get a battery’s voltage but you can also get the temperature, status and even an icon representing the current state of the battery.
In this post we’ll make an app that display’s all of the device’s battery information in a text view whenever there is a change of state in the battery.
The App’s Interface
Our interface will consists of a simple text view where we will show the battery’s information and an image view where we will display the battery’s current state using the image provided by the API to do this.
the result: an app showing the device's battery information
Receiving Battery Updates
In order to receive the updated information from the battery we need to register a broadcast receiver and an intent filter. An intent filter tells the native battery app that our app is listening for its changes.
This is how we register a receiver with an intent filter:
this.registerReceiver(this.batteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
In the code above the receiver is an object we are going to make called batteryInfoReceiver, the intent filter is listening for the action battery changed.
Our receiver object is the following:
private BroadcastReceiver batteryInfoReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { // this is where we deal with the data sent from the battery. } };
Battery Manager API
If you look at the battery manager api yo see that it has many constants that start with the word EXTRA_, these extras is the data we are able to get from the battery and also the type (int, String etc).
Storing the battery info in variables with matching types int health= intent.getIntExtra(BatteryManager.EXTRA_HEALTH,0); int icon_small= intent.getIntExtra(BatteryManager.EXTRA_ICON_SMALL,0); int level= intent.getIntExtra(BatteryManager.EXTRA_LEVEL,0); int plugged= intent.getIntExtra(BatteryManager.EXTRA_PLUGGED,0); boolean present= intent.getExtras().getBoolean(BatteryManager.EXTRA_PRESENT); int scale= intent.getIntExtra(BatteryManager.EXTRA_SCALE,0); int status= intent.getIntExtra(BatteryManager.EXTRA_STATUS,0); String technology= intent.getExtras().getString(BatteryManager.EXTRA_TECHNOLOGY); int temperature= intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,0); int voltage= intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE,0);
We can easily display the data in the views, the icon_small variable holds a resource id with the battery’s image:
Displaying data in text and image view batteryInfo.setText( "Health: "+health+"\n"+ "Icon Small:"+icon_small+"\n"+ "Level: "+level+"\n"+ "Plugged: "+plugged+"\n"+ "Present: "+present+"\n"+ "Scale: "+scale+"\n"+ "Status: "+status+"\n"+ "Technology: "+technology+"\n"+ "Temperature: "+temperature+"\n"+ "Voltage: "+voltage+"\n"); imageBatteryState.setImageResource(icon_small);
This is the full Java code.
BatteryInformation.java package com.yoursite.batteryinformation; 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.widget.ImageView; import android.widget.TextView; public class BatteryInformation extends Activity { private TextView batteryInfo; private ImageView imageBatteryState; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); batteryInfo=(TextView)findViewById(R.id.textViewBatteryInfo); imageBatteryState=(ImageView)findViewById(R.id.imageViewBatteryState); this.registerReceiver(this.batteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); } private BroadcastReceiver batteryInfoReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { int health= intent.getIntExtra(BatteryManager.EXTRA_HEALTH,0); int icon_small= intent.getIntExtra(BatteryManager.EXTRA_ICON_SMALL,0); int level= intent.getIntExtra(BatteryManager.EXTRA_LEVEL,0); int plugged= intent.getIntExtra(BatteryManager.EXTRA_PLUGGED,0); boolean present= intent.getExtras().getBoolean(BatteryManager.EXTRA_PRESENT); int scale= intent.getIntExtra(BatteryManager.EXTRA_SCALE,0); int status= intent.getIntExtra(BatteryManager.EXTRA_STATUS,0); String technology= intent.getExtras().getString(BatteryManager.EXTRA_TECHNOLOGY); int temperature= intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,0); int voltage= intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE,0); batteryInfo.setText( "Health: "+health+"\n"+ "Icon Small:"+icon_small+"\n"+ "Level: "+level+"\n"+ "Plugged: "+plugged+"\n"+ "Present: "+present+"\n"+ "Scale: "+scale+"\n"+ "Status: "+status+"\n"+ "Technology: "+technology+"\n"+ "Temperature: "+temperature+"\n"+ "Voltage: "+voltage+"\n"); imageBatteryState.setImageResource(icon_small); } }; }
Battery Permissions
No special permissions are needed in the manifest file.
0 comments:
Post a Comment
Thank you for your comment.