Detecting whether a headset is plugged into an Android device or not.
When you say "headset", do you mean "wired headset"? If so, there's an intent to detect whether or not one is being plugged or unplugged: ACTION_HEADSET_PLUG
.
To check the status, you can use AudioManager.isWiredHeadsetOn()
, although that may return false if there is also a bluetooth headset, and audio is routed to that instead.
Check whether headphones are plugged in
You can use this code for checking if the headset is plugged in
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.isWiredHeadsetOn();
(Don't worry about the deprecation, it's still usable for ONLY checking if the headset are plugged in.)
And you need <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
Available in Android 2.0 +
How to detect when a user plugs headset on android device? (Opposite of ACTION_AUDIO_BECOMING_NOISY)
How about this call:
http://developer.android.com/reference/android/content/Intent.html#ACTION_HEADSET_PLUG
which I found at
Droid Incredible Headphones Detection
?
The updated code I see in your question now isn't enough. That broadcast happens when the plugged state changes, and sometimes when it doesn't, according to Intent.ACTION_HEADSET_PLUG is received when activity starts so I would write:
package com.example.testmbr;
import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
private MusicIntentReceiver myReceiver;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myReceiver = new MusicIntentReceiver();
}
@Override public void onResume() {
IntentFilter filter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
registerReceiver(myReceiver, filter);
super.onResume();
}
private class MusicIntentReceiver extends BroadcastReceiver {
@Override public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
int state = intent.getIntExtra("state", -1);
switch (state) {
case 0:
Log.d(TAG, "Headset is unplugged");
break;
case 1:
Log.d(TAG, "Headset is plugged");
break;
default:
Log.d(TAG, "I have no idea what the headset state is");
}
}
}
}
@Override public void onPause() {
unregisterReceiver(myReceiver);
super.onPause();
}
}
The AudioManager.isWiredHeadsetOn() call which I earlier recommended turns out to be deprecated since API 14, so I replaced it with extracting the state from the broadcast intent. It's possible that there could be multiple broadcasts for each plugging or unplugging, perhaps because of contact bounce in the connector.
Android: Checking if headphones are plugged in
It looks like you'll be interested in the isWiredHeadsetOn()
method and isBluetoothA2dpOn()
method of the AudioManager
class.
However, the isWiredHeadsetOn()
method is only available in Android 2.0 or later. (The isBluetoothA2dpOn()
method has been available since Android 1.5.)
Check whether the Headphone is plugged in android phone
You can use BroadcastReceiver
public class MainActivity extends AppCompatActivity {
private MusicIntentReceiver myReceiver;
boolean isHeadphoneConnected;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
MusicIntentReceiver myReceiver = new MusicIntentReceiver();
IntentFilter filter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
registerReceiver(myReceiver, filter);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(isHeadphoneConnected)
Toast.makeText(MainActivity.this,"Headset is plug in",Toast.LENGTH_LONG).show();
else
Toast.makeText(MainActivity.this,"Headset is not plug in",Toast.LENGTH_LONG).show();
}
});
}
private class MusicIntentReceiver extends BroadcastReceiver {
@Override public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
int state = intent.getIntExtra("state", -1);
switch (state) {
case 0:
isHeadphoneConnected = false;
break;
case 1:
isHeadphoneConnected = true;
break;
}
}
}
}
}
Detect plugged & unplugged event on headphone jack in Xamarin.Android
you can try to convert java code into c#, for example:
[BroadcastReceiver(Enabled = true)]
[IntentFilter(new[] { Android.Content.Intent.ActionHeadsetPlug })]
public class MyBroadcastReceiver : BroadcastReceiver
{
bool Microphone_Plugged_in = false;
public override void OnReceive(Context context, Intent intent)
{
String action = intent.Action;
int iii;
if (Intent.ActionHeadsetPlug.Equals(action))
{
iii = intent.GetIntExtra("state", -1);
if (iii == 0)
{
Microphone_Plugged_in = false;
Toast.MakeText(context, "microphone not plugged in", ToastLength.Long).Show();
}
if (iii == 1)
{
Microphone_Plugged_in = true;
Toast.MakeText(context, "microphone plugged in", ToastLength.Long).Show();
}
}
}
}
And usage:
BroadcastReceiver broadcastReceiver;
IntentFilter receiverFilter;
and initialize value in method OnCreate
of your activity:
broadcastReceiver = new MyBroadcastReceiver();
receiverFilter = new IntentFilter(Intent.ActionHeadsetPlug);
And RegisterReceiver
and UnregisterReceiver
by override method OnResume
and OnPause
protected override void OnResume()
{
base.OnResume();
RegisterReceiver(broadcastReceiver, receiverFilter);
}
protected override void OnResume()
{
base.OnResume();
IntentFilter receiverFilter = new IntentFilter(Intent.ActionHeadsetPlug);
RegisterReceiver(broadcastReceiver, receiverFilter);
}
For more, you can check thread: Detecting whether a headset is plugged into an Android device or not.
https://docs.microsoft.com/en-us/xamarin/android/app-fundamentals/broadcast-receivers
Detect if bluetooth headset connected
Thanks for the answer @cristallo
This is the way I handled it.
Connect to proxy
bluetoothAdapter.getProfileProxy(mContext, mProfileListener, BluetoothProfile.HEADSET);
Created a listener
private BluetoothProfile.ServiceListener mProfileListener = new BluetoothProfile.ServiceListener()
{
@Override
public void onServiceConnected(int profile, BluetoothProfile proxy)
{
if (profile == BluetoothProfile.HEADSET)
{
mBluetoothHeadset = (BluetoothHeadset) proxy;
if(mBluetoothHeadset.getConnectedDevices().size()>0) {
IS_BLUETOOTH_CONNECTED = true;
Logs.d(TAG,"Bluetooth device is connected");
}
}
}
@Override
public void onServiceDisconnected(int profile)
{
if (profile == BluetoothProfile.HEADSET)
{
mBluetoothHeadset = null;
IS_BLUETOOTH_CONNECTED = false;
Logs.d(TAG,"Bluetooth device is disconnected");
}
}
};
Referred from Detect programatically if headphone or bluetooth headset attached with android phone
The blogger Vipul had created a BluetoothHeadsetManager class, which handles getting the headset profile, handling the listener, checking if the bluetooth is enabled or not.I haven't utilized the Broadcast Receiver.
switch (audioMgr.getRingerMode()) {
case AudioManager.RINGER_MODE_SILENT:
if(BluetoothHeadsetManager.IS_BLUETOOTH_CONNECTED) {
//play notification
}
break;
case AudioManager.RINGER_MODE_VIBRATE:
if(BluetoothHeadsetManager.IS_BLUETOOTH_CONNECTED) {
//play notification
}
break;
case AudioManager.RINGER_MODE_NORMAL:
//play ringtone
break;
default:
break;
}}
Related Topics
Read Only File System on Android
Letting Webview on Android Work with Prefers-Color-Scheme: Dark
How to Set Margin of Imageview Using Code, Not Xml
Apply a Theme to an Activity in Android
Out of Memory Error with Bitmap
Android Imagebutton with a Selected State
Android - How to Unregister a Receiver Created in the Manifest
Android Layout Replacing a View with Another View on Run Time
How to Start Fragment from an Activity
How to View Androidmanifest.Xml from APK File
How Much Support Is There for -Webkit-Overflow-Scrolling:Touch
Transparent Actionbar: Custom Tabcolor
Transitive Dependencies for Local Aar Library
Windowsoftinputmode="Adjustresize" Not Working with Translucent Action/Navbar
How to Check If an Activity Is the Last One in the Activity Stack for an Application