How to check the charger is plugin or not in android

Android provides the features by which you can check the charger connectivity in android.

In my prgram run the app it will show you the one button when you click on button it will give the message charger is connected or not with phone.

Charger connectivity:.

In this section i am going to show you the charger connectivity with practical example.

First open the android studio and create a project and give the name of your project like ChargerConnectivityDemo.

The screenshots of the program is as follows:

charger connectivity image charger connectivity image

You need to drag and drop the one Button,two TextView from pallet. Open the activity_main.xml file and code look like this

<LinearLayout xmlns:android=""

    android:text="Check Charger Connectivity"


Open the MainActivity java class and write code look like this.

package com.example.niitpc.myapplication;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import java.util.Locale;

public class MainActivity extends AppCompatActivity   {

static TextView adapterStatus,currentStatus;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView(R.layout.activity_main );
        adapterStatus=(TextView)findViewById( );
        currentStatus=(TextView)findViewById( );

public static void  isPlugged(Context context) {

Intent intent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
String action = intent.getAction();
if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
int remain = intent.getIntExtra( BatteryManager.EXTRA_LEVEL, 0 );
String remainString = Integer.toString( remain ) + "%";
currentStatus.setText( remainString );

int plugIn = intent.getIntExtra( BatteryManager.EXTRA_PLUGGED, 0 );
switch (plugIn) {
case 0:
   adapterStatus.setText( "No Connection" );

case BatteryManager.BATTERY_PLUGGED_AC:
  adapterStatus.setText( "Adapter Connected" );

case BatteryManager.BATTERY_PLUGGED_USB:
  adapterStatus.setText( "USB Connected" );
public void Connectity(View v)
   isPlugged( this );




static TextView adapterStatus,currentStatus:Here i have declared two TextView object adapterStatus and currentStatus.

adapterStatus=(TextView)findViewById( ):Here i have initialized the adapterStaus object.

currentStatus=(TextView)findViewById( ):Here i have initialized the currentStaus object.

public static void isPlugged(Context context):Here i have created the static isPlugged() and it will take one argument context.

Intent intent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)):Here we call the registerReceiver with the help of context the registerReceiver return the intent and this method take two argument first is null and second the IntentFilter object and their constructor take the Intent.ACTION_BATTERY_CHANGED as argument that indicate the integer containing the current health constant.

String action= intent.getAction():here we call the getAction()returns a pointer id and an event (i.e., up, down, move) information.

if (action.equals(Intent.ACTION_BATTERY_CHANGED)):The action variable check the if this is equal to Intent.ACTION_BATTERY_CHARGED.

int remain = intent.getIntExtra( BatteryManager.EXTRA_LEVEL, 0 ):The getIntExtra() take the argument BatteryManager.EXTRA_LEVEL and second is 0 this method return the remain battery percentage.

String remainString = Integer.toString( remain ) + "%":Here we convert the remain variable into String and concat the % symbol and assign into to the remainString variable.

int plugIn = intent.getIntExtra( BatteryManager.EXTRA_PLUGGED, 0 ):The getIntExtra() take the argument BatteryManager.EXTRA_PLUGGED and second is 0 this method return the constant int value for example .

switch (plugIn):Here we use the switch case and pass the plugIn variable to compare with extra plugged is connected or not like if no charger the default value is 0.

case 0:First case is 0.

adapterStatus.setText( "No Connection" ):This statement set the text as No Connection if first case 0 is match.

case BatteryManager.BATTERY_PLUGGED_AC:Here we match the plugIn variable value with BatteryManager.BATTERY_PLUGGED_IN_AC if match then.

adapterStatus.setText( "Adapter Connected" ):This statement set the text as Adapter Connected if case 0 is match with BatteryManager.BATTERY_PLUGGED_IN_AC.

case BatteryManager.BATTERY_PLUGGED_USB:Here we match the plugIn variable value with BatteryManager.BATTERY_PLUGGED_IN_USB if match then.

adapterStatus.setText( "USB Connected" ):This statement set the text as USB Connected if case is match with BatteryManager.BATTERY_PLUGGED_IN_USB.

public void Connectity(View v):Here i have created the Connectivity().

isPlugged( this ):Here i have called the isPlugged() inside the Connectivity().

You must call the Connectivity() to android:onClick of the button.

check the charger connectivity

Check the charger connectivity for video click here

back                     next