Friday, April 3, 2015

Read battery status and level

Android example to read battery status and level via IntentFilter(Intent.ACTION_BATTERY_CHANGED).


package com.example.androidbattery;

import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Bundle;

public class MainActivity extends ActionBarActivity {

 Button btnReadBattery;
 TextView textBatteryStatus;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        btnReadBattery = (Button)findViewById(R.id.readbattery);
        textBatteryStatus = (TextView)findViewById(R.id.batterystatus);
        
        btnReadBattery.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    textBatteryStatus.setText(readBattery());
   }});
        
    }

    private String readBattery(){
     StringBuilder sb = new StringBuilder();
     IntentFilter batteryIntentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
     Intent batteryIntent = registerReceiver(null, batteryIntentFilter);

     int status = batteryIntent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
     if(status == BatteryManager.BATTERY_STATUS_CHARGING){
      sb.append("BATTERY_STATUS_CHARGING\n");
     }
     if(status == BatteryManager.BATTERY_STATUS_FULL){
      sb.append("BATTERY_STATUS_FULL\n");
     }

     int plugged = batteryIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
     if(plugged == BatteryManager.BATTERY_PLUGGED_USB){
      sb.append("BATTERY_PLUGGED_USB\n");
     }
     if(plugged == BatteryManager.BATTERY_PLUGGED_AC){
      sb.append("BATTERY_PLUGGED_AC\n");
     }

     int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
     sb.append("level: " + level + "\n");
     
        int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
        sb.append("scale: " + scale + "\n");
        
        return sb.toString();
    }
}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.androidbattery.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />

    <Button
        android:id="@+id/readbattery"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Read Battery" />

    <TextView
        android:id="@+id/batterystatus"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>


Next:
- Get battery information using BatteryManager

No comments: