Close

Android Code to get Device’s Battery Status

  • Home
  •  / 
  • ANDROID
  •  / 
  • Android Code to get Device’s Battery Status

Android Code to get Device’s Battery Status

Step 1: Create Project in Android Studio and Put Following code  in your xml file 

 

<?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/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="30dp"
   >

    <TextView
        android:id="@+id/textViewBatteryStatus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Current Battery Status"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/buttonBatteryStatus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textViewBatteryStatus"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="16dp"
        android:visibility="gone"
        android:text="Click here to Get battery Info" />
</RelativeLayout>




Step 2: Create Activity and put following java code

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

public class Battery extends Activity {

    TextView batterystatustextview;
    Button buttongetstatus;
    IntentFilter batteryintentfilter;
    int deviceStatus;
    String currentBatteryStatus="Your Phone Battery Status";
    int batteryLevel;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_battery);

        buttongetstatus = (Button)findViewById(R.id.buttonBatteryStatus);
        batterystatustextview = (TextView)findViewById(R.id.textViewBatteryStatus);

        batteryintentfilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        Battery.this.registerReceiver(broadcastreceiver,batteryintentfilter);

    }

    private BroadcastReceiver broadcastreceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            deviceStatus = intent.getIntExtra(BatteryManager.EXTRA_STATUS,-1);
            int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
            int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
            int batteryLevel=(int)(((float)level / (float)scale) * 100.0f);

            if(deviceStatus == BatteryManager.BATTERY_STATUS_CHARGING){

                batterystatustextview.setText(currentBatteryStatus+" = Charging"+batteryLevel+" %");

            }

            if(deviceStatus == BatteryManager.BATTERY_STATUS_DISCHARGING){

                batterystatustextview.setText(currentBatteryStatus+batteryLevel+" %");

            }

            if (deviceStatus == BatteryManager.BATTERY_STATUS_FULL){

                batterystatustextview.setText(currentBatteryStatus+"= Battery Full at "+batteryLevel+" %");

            }

            if(deviceStatus == BatteryManager.BATTERY_STATUS_UNKNOWN){

                batterystatustextview.setText(currentBatteryStatus+" = Unknown at "+batteryLevel+" %");
            }


            if (deviceStatus == BatteryManager.BATTERY_STATUS_NOT_CHARGING){

                batterystatustextview.setText(currentBatteryStatus+batteryLevel+" %");

            }

        }
    };
}

Step 3: Run the Code And get Battery Status.
        "Happy Coding"
 

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *