Thursday, December 3, 2015

android - How to get battery voltage

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="#cce9cf"
    >
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30dp"
        />
</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;

import java.text.DecimalFormat;


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_VOLTAGE
                    Extra for ACTION_BATTERY_CHANGED: integer containing the current
                    battery voltage level.

                    Constant Value: "voltage"
            */

            // Get the battery voltage
            // Current battery voltage in Millivolts
            int voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE,0);

            // Convert Millivolts to Volts
            double volt =  voltage * 0.001;

            // Initialize a new DecimalFormat instance
            DecimalFormat newFormat = new DecimalFormat("#.#");

            // Format the decimal value to one decimal position
            double oneDecimalVolt =  Double.valueOf(newFormat.format(volt));

            // Display the battery voltage in TextView
            mTextView.setText("Battery Voltage");

            // Display the battery voltage in Millivolt unit
            mTextView.setText(mTextView.getText() +"\nMillivolts : " + voltage);

            // Display the battery voltage in Volt unit
            mTextView.setText(mTextView.getText() + "\nVolt : " + oneDecimalVolt);
        }
    };

    @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