Tuesday, 28 April 2015

Display Battery Status - Android



package com.example.getbetterylevel;

import java.io.IOException;

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

public class MainActivity extends Activity {

private BroadcastReceiver mbcr = new BroadcastReceiver() {
// onReceive method will receive updates
public void onReceive(Context c, Intent i) {
// initially level has 0 value
// after getting update from broadcast receiver
// it will change and give battery status
int level = i.getIntExtra("level", 0);
// initialize all objects
ProgressBar pb = (ProgressBar) findViewById(R.id.progressBar1);
TextView tv = (TextView) findViewById(R.id.textView1);
// set level of progress bar
pb.setProgress(level);
// display level on text view
tv.setText("Batterylevel:" + Integer.toString(level) + "%");
// start a song when the battery level touches 100%
if (level == 100) {

//do something on bettery full charged
}
}
};

/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set layout
setContentView(R.layout.activity_main);
// register broadcast receiver
// Get battery changed status
// we can get more options like power connect, disconnect, call, etc.
// To get more options, write Intent followed by a dot(.) and press
// CTRL+Space
// you will get all the options
registerReceiver(mbcr, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}
}

activity_main.xml

<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/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text=""
        android:textSize="25sp" />

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleHorizontal"
        android:progressDrawable="@drawable/progressbar2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:minHeight="100dp"
        android:minWidth="200dp" />

</LinearLayout>


drawable->progressbar2.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
  <item android:id="@android:id/background">
    <shape>
      <corners android:radius="5dip" />
      <gradient
        android:angle="270"
        android:centerColor="#ff5a5d5a"
        android:centerY="0.5"
        android:endColor="#ff747674"
        android:startColor="#ff9d9e9d" />
    </shape>
  </item>
  <item android:id="@android:id/progress">
    <clip>
      <shape>
        <corners android:radius="5dip" />
          <gradient
            android:angle="0"
            android:endColor="#ff009900"
            android:startColor="#ff000099" />
      </shape>
    </clip>
  </item>
</layer-list>