ACTION_HEADSET_PLUG Intent In Android

Introduction 

Generally, when we connect headphones to our android device then a headphone icon is visible in the status bar of the android device. Similarly, when we disconnect the headphone from the android device the headphone icon is not visible in the status bar. We can perform this task in android studio by using the  ACTION_HEADSET_PLUG Intent.

Implementation

We can implement this task in Android studio. We have to use the BrodcastReceiver component and Intentfilter. The intentfilter specifies the types of intents that the component would like to receive. Component may be activity, service, or broadcast receiver, etc. 

Step 1 

Create a new project in Android Studio and select the empty Activity.

 

Click on finish Button.

Step 2

  • Download a Headset Icon from any website and paste it into the drawable folder in the project structure.
  • Follow the below step to paste image into drawable folder.
  • First copy the icon image and go to android studio, Right click on Drawable folder and go to paste option and paste it.
  • After this a new pop window is open as like below picture, just simply click on ok.

 

Step 3

Go to activity_main.xml  and add an ImageView component. By default, we have a textview so first need to delete it and then add the ImageView.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/img"
        android:visibility="gone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/headset"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

In activity_main.xml, we add an ImageView and set its id and visibility. Initially, we assume that headphone is not plugged in so we set the visibility of ImageView as gone.

Step 4

Now go to Main Activity and create ImageView and IntentFilter objects.

ImageView imageView;
IntentFilter intentFilter;

Step 5

Inside onCreate() method, Initialize ImageView and IntentFilter objects by using the findViewById() Method.

imageView=findViewById(R.id.img);
intentFilter= new IntentFilter(Intent.ACTION_HEADSET_PLUG);

Here, we pass an Intent.ACTION_HEADSET_PLUG as a parameter in the intentFilter object.

Step 6

Now we have to create a BroadcastReceiver object and provide the definition of this object in MainActivty.java.

BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {}
};

In android operating system, we encounter different types of events. These events could be Battery Low, Wi-Fi availability, Incoming call, Incoming SMS, Battery low or charged, headphones connected or disconnected, etc. The BroadcastReceiver component listens to these events. These events are nothing but an intent. 

Step 7

Now we have to write the condition for whether the headphone is plugged in or not. We have to write this condition inside onReceive() method.

final String action = intent.getAction();
if (Intent.ACTION_HEADSET_PLUG.equals(action)) {
    if (intent.getIntExtra("state", -1) == 0) {
        imageView.setVisibility(View.GONE);
    } else {
        imageView.setVisibility(View.VISIBLE);
        Toast.makeText(context, "plug in", Toast.LENGTH_LONG).show();
    }
}

First, we get the action of intent by using getAction() method. Now write condition for when headphone is not connected then set visiblity gone. If headphone is connected then set visibility visible.

Step 8

Now we have to create onResume() method and call the registerReceiver() method.

protected void onResume() {
    super.onResume();
    registerReceiver(broadcastReceiver, intentFilter);
}

We have to pass two parameters in registerReceiver() method.

  • IntentFilter Object 
  • BroadcastReceiver Object

Step 9

Now we have to create onPause() method and call the unregisterReceiver() method. 

protected void onPause() {
    super.onPause();
    unregisterReceiver(broadcastReceiver);
    imageView.setVisibility(View.GONE);
}

Output

 

Conclusion

In this article, we saw how we can use ACTION_HEADSET_PLUG Intent in Android. We also learn how to use the visibility attribute of Views in our android project.

Thanks for reading and hope you like it. If you have any suggestion/query on this article. Please share your thoughts.


Similar Articles