android detects whether the headset is plugged into the method

  • 2020-05-07 20:23:31
  • OfStack

AudioManager has this method:
isWiredHeadsetOn ();
If headphones are plugged in, true is returned, otherwise false;
Of course, add a permission, or 1 will return false.
< uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" / >
I started chasing the source code for a long time. Found real-time detection of headphone insertion and unplugging, but it didn't help much with my needs.
Real-time detection of earphone insertion and withdrawal:
Every time you plug in and pull out your headphones, the system sends an Intent broadcast,
So, only need to use a receiver the radio intent (get action is: "... android intent action HEADSET_PLUG ") intercept.
This receiver must be registered in code, not written to memory in manifest.
Was realized under Android headset insert and pull out, also namely, to establish a Broadcast Receiver, listening "android. intent. action. HEADSET_PLUG" broadcast
But just add one to AndroidManifest.xml < receiver > The label is invalid, such as:
[html]
 
<receiver android:name=".HeadsetPlugReceiver"> 
<intent-filter> 
<action android:name="android.intent.action.HEADSET_PLUG" android:enabled="true"></action> 
</intent-filter> 
</receiver> 

You will find that Receiver's onReceive event will never be triggered, and the solution is to manually write the code to register the broadcast.
First, create a subclass of BroadcastReceiver to listen for headphones to be plugged in and pulled out:
[java]
 
public class HeadsetPlugReceiver extends BroadcastReceiver { 
private static final String TAG = "HeadsetPlugReceiver"; 
@Override 
public void onReceive(Context context, Intent intent) { 
if (intent.hasExtra("state")){ 
if (intent.getIntExtra("state", 0) == 0){ 
Toast.makeText(context, "headset not connected", Toast.LENGTH_LONG).show(); 
} 
else if (intent.getIntExtra("state", 0) == 1){ 
Toast.makeText(context, "headset connected", Toast.LENGTH_LONG).show(); 
} 
} 
} 
} 

Then, register to listen to the broadcast in onCreate() in Activity where you need to listen for the event, and don't forget to log off from onDestroy() :
[java]
 
public class TestHeadSetPlugActivity extends Activity { 
private HeadsetPlugReceiver headsetPlugReceiver; 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 
/* register receiver */ 
registerHeadsetPlugReceiver(); 
} 
private void registerHeadsetPlugReceiver() { 
headsetPlugReceiver = new HeadsetPlugReceiver(); 
IntentFilter intentFilter = new IntentFilter(); 
intentFilter.addAction("android.intent.action.HEADSET_PLUG"); 
registerReceiver(headsetPlugReceiver, intentFilter); 
} 
@Override 
public void onDestroy() { 
unregisterReceiver(headsetPlugReceiver); 
super.onDestroy(); 
} 
} 

As mentioned above it can be realized to detect the earphone plug in and out.

Related articles: