Skip to main content

android - How to get battery status programmatically

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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:padding="16dp"
    tools:context=".MainActivity"
    android:background="#e7e6ff"
    >
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30dp"
        android:textColor="#000"
        />
</RelativeLayout>
MainActivity.java

package com.cfsuman.me.androidcode;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {
    private Context mContext;
    private TextView mTextView;

    // Initialize a new BroadcastReceiver instance
    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(mContext, "Received", Toast.LENGTH_SHORT).show();

            /*
                BatteryManager
                    The BatteryManager class contains strings and constants used for values in the
                    ACTION_BATTERY_CHANGED Intent, and provides a method for querying battery
                    and charging properties.
            */
            /*
                public static final String EXTRA_STATUS
                    Extra for ACTION_BATTERY_CHANGED: integer containing the current
                    status constant.

                    Constant Value: "status"
            */

            // Get the battery status indicator integer value
            int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS,-1);

            // Initialize a new String to hold battery status
            String statusString = "";

            // Determine the battery status from the return integer value
            if(status == BatteryManager.BATTERY_STATUS_UNKNOWN){
                /*
                    public static final int BATTERY_STATUS_UNKNOWN
                        Constant Value: 1 (0x00000001)
                */
                statusString = "UNKNOWN";
            }else if(status == BatteryManager.BATTERY_STATUS_CHARGING){
                /*
                    public static final int BATTERY_STATUS_CHARGING
                        Constant Value: 2 (0x00000002)
                */
                statusString = "CHARGING";
            }else if(status == BatteryManager.BATTERY_STATUS_DISCHARGING){
                /*
                    public static final int BATTERY_STATUS_DISCHARGING
                        Constant Value: 3 (0x00000003)
                */
                statusString = "DISCHARGING";
            }else if (status == BatteryManager.BATTERY_STATUS_NOT_CHARGING){
                /*
                    public static final int BATTERY_STATUS_NOT_CHARGING
                        Constant Value: 4 (0x00000004)
                */
                statusString = "NOT CHARGING";
            }else if (status == BatteryManager.BATTERY_STATUS_FULL){
                /*
                    public static final int BATTERY_STATUS_FULL
                        Constant Value: 5 (0x00000005)
                */
                statusString = "FULL";
            }

            // Display the battery status in TextView
            mTextView.setText("Battery Status : " + statusString);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // Request window feature action bar
        requestWindowFeature(Window.FEATURE_ACTION_BAR);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the application context
        mContext = getApplicationContext();

        // Initialize a new IntentFilter instance
        IntentFilter iFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);

        // Register the broadcast receiver
        mContext.registerReceiver(mBroadcastReceiver,iFilter);

        // Get the widgets reference from XML layout
        mTextView = (TextView) findViewById(R.id.tv);
    }
}
More android examples

Popular posts from this blog

Android Popup Window Example

activity_main.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/rl" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp" tools:context=".MainActivity" android:background="#f5f1e0" > <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Show Popup Window" /> </RelativeLayout> res/layout/custom_layout.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/rl_custom_layout" …

How to add a hint to Spinner in Android

activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/rl" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp" tools:context=".MainActivity" android:background="#60c4ff" > <Spinner android:id="@+id/spinner" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout> spinner_item.xml <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:textStyle="italic" …

How to create a round/circle Button in Android

activity_main.xml code snippet <Button android:id ="@+id/push_button" android:layout_width="150dp" android:layout_height="150dp" android:text="Round Button" android:background="@drawable/button_bg_round" android:padding="15dp" /> res/drawable/button_bg_round.xml <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape android:shape="oval"> <stroke android:color="#1E90FF" android:width="5dp" /> <solid android:color="#87CEEB"/> <size android:width="150dp" android:height="150dp"/> </shape> </item> </selector> More android examples How to set Button fontHow to change Button font programmaticallyHow to set Button text style to bold and italic…