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="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    tools:context=".MainActivity">

<Button
    android:id="@+id/btncharger"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Check Charger Connectivity"
    android:background="@android:color/holo_red_dark"
    android:textColor="@android:color/white"
    android:onClick="Connectity"
        />

</LinearLayout>
       

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.support.v7.app.AppCompatActivity;
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;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView(R.layout.activity_main );
        adapterStatus=(TextView)findViewById( R.id.adap );
        currentStatus=(TextView)findViewById( R.id.currentStatus );


    }
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" );
   break;

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

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

 }



}



Explanation:

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

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

currentStatus=(TextView)findViewById( R.id.currentStatus ):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