Friday, 5 June 2015

Android battery level indicator

No comments
In this android example we write a simple code to get battery level in percentage.before to write code you need to know the basics of  broadcaster receiver first. let me explain the stuffs about it.


- Intent.Action_Battery_Changed : This is a sticky broadcast containing the charging state, level, and other information about the battery.The android BatteryManager class contains strings and constants used for values in the ACTION_BATTERY_CHANGED Intent. Among the various constants available.

- registerReceiver(BroadcastReceiver receiver, IntentFilter filter) : We need to register for Action_Battery_Changed broadcast in this method. This will get invoked whenever this event occurs. Since this is a sticky Intent, it keeps on broadcasting once registered.

- String EXTRA_HEALTH: integer containing the current health constant.

- String EXTRA_ICON_SMALL: integer containing the resource ID of a small status bar icon indicating the current battery state.

- EXTRA_LEVEL: integer field containing the current battery level, from 0 to EXTRA_SCALE.

- EXTRA_PLUGGED: integer indicating whether the device is plugged in to a power source; 0 means it is on battery, other constants are different types of power sources.

- EXTRA_PRESENT: Boolean indicating whether a battery is present.

- EXTRA_SCALE: integer containing the maximum battery level.

- EXTRA_STATUS: integer containing the current status constant.

- EXTRA_TECHNOLOGY: String describing the technology of the current battery.

- EXTRA_TEMPERATURE: integer containing the current battery temperature.

- EXTRA_VOLTAGE: integer containing the current battery voltage level.

BATTERY_PLUGGED_AC: Power source is an AC charger.

- BATTERY_PLUGGED_USB: Power source is an  USB charger.

To know More About CLICK HERE:

I hope all above stuff is helpful for you.Now let's go for the code.

Step 1: Write code into activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textfield"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="40dip" />

    <ProgressBar
        android:id="@+id/progressbar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="20dip"
        android:max="100"
        android:maxHeight="500dip"
        android:maxWidth="300dip"
        android:minHeight="100dip"
        android:minWidth="200dip" />

</LinearLayout>

Step 2: Write code into MainActivity.java

package dev.androidapplink.batterylevelapp;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MainActivity extends Activity {

private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context c, Intent i) {
int level = i.getIntExtra("level", 0);
ProgressBar pb = (ProgressBar) findViewById(R.id.progressbar);
pb.setProgress(level);
TextView tv = (TextView) findViewById(R.id.textfield);
tv.setText("Battery Level: " + Integer.toString(level) + "%");
}

};

// Called when the activity is first created. 
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
registerReceiver(mBatInfoReceiver, new IntentFilter(
Intent.ACTION_BATTERY_CHANGED));
}
}

NOTE:No need any permission in AndroidManifest.xml 

Step 3: Run your project:


No comments :

Post a Comment

Follow me Share