Thursday, December 3, 2015

android - How to get battery charging state and method

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="#b6c9a5"
    >
    <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);

            // Determine the battery charging status
            boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING
                                 ||
                                 status == BatteryManager.BATTERY_STATUS_FULL;

            // If battery is in charging states, then get the charging method
            if(isCharging){
                // Display the battery charging states
                mTextView.setText("Charging : Yes.");

                /*
                    public static final String EXTRA_PLUGGED
                        Extra for ACTION_BATTERY_CHANGED: 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.

                        Constant Value: "plugged"
                */
                int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED,-1);

                /*
                    public static final int BATTERY_PLUGGED_USB
                        Power source is a USB port.
                        Constant Value: 2 (0x00000002)
                */
                // Determine that the battery power source is usb or not
                boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
                if(usbCharge){
                    mTextView.setText(mTextView.getText()+"\nUSB Charging");
                }

                /*
                    public static final int BATTERY_PLUGGED_AC
                        Power source is an AC charger.
                        Constant Value: 1 (0x00000001)
                */
                // Determine that the battery power source is ac or not
                boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
                if(acCharge){
                    mTextView.setText(mTextView.getText()+"\nAC Charging");
                }

                /*
                    public static final int BATTERY_PLUGGED_WIRELESS
                        Added in API level 17
                        Power source is wireless.
                        Constant Value: 4 (0x00000004)
                */
                // Determine that the battery power source is wireless or not
                boolean wirelessCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_WIRELESS;
                if(wirelessCharge){
                    mTextView.setText(mTextView.getText()+"\nWireless Charging");
                }
            }else {
                // Display the battery charging state
                mTextView.setText("Charging : No.");
            }
        }
    };

    @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