Android detects whether the headset is inserted

Source: Internet
Author: User

AudioManager has the following method:
IsWiredHeadsetOn ();
If the headset is inserted, true is returned; otherwise, false is returned;
Of course, you must add a permission. Otherwise, false is always returned.
<Uses-permission android: name = "android. permission. MODIFY_AUDIO_SETTINGS"/>
I started to catch up with the source code for a long time. I found the process of real-time detecting the insertion and removal of headphones, but it is not very helpful for me.
Real-time Monitoring of headset insertion and removal:
The system sends Intent broadcast every time the headset is inserted and pulled out,
Therefore, you only need to use a receiver to intercept the broadcast intent (the obtained action is android. intent. action. HEADSET_PLUG.
This aggreger must be registered using code, but not written into manifest using the memory writing method.
Detects the insertion and unplugging of headphones in Android, that is, creating a Broadcast Receiver to listen to "android. intent. action. HEADSET_PLUG" Broadcast.
However, adding a <receiver ER> tag directly to AndroidManifest. xml is invalid, for example:
[Html] Copy codeThe Code is as follows: <Cycler android: name = ". headsetplugreceivreceiver">
<Intent-filter>
<Action android: name = "android. intent. action. HEADSET_PLUG" android: enabled = "true"> </action>
</Intent-filter>
</Cycler>

You will find that the onReceive event of the Receiver will never be triggered. The solution is to manually write the code to register the broadcast.
First, create a subclass of BroadcastReceiver to listen to the plug-in and pull-out of the headset:
[Java]Copy codeThe Code is as follows: public class HeadsetPlugReceiver extends BroadcastReceiver {
Private static final String TAG = "headsetplugreceivreceiver ";
@ 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 and listen to the broadcast in onCreate () of the Activity that needs to listen to the event, and do not forget to log off listening to the broadcast in onDestroy:
[Java]Copy codeThe Code is as follows: public class TestHeadSetPlugActivity extends Activity {
Private HeadsetPlugReceiver headsetplugreceivreceiver;
/** 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 registerheadsetplugreceivreceiver (){
Headsetplugreceivreceiver = new headsetplugreceivreceiver ();
IntentFilter intentFilter = new IntentFilter ();
IntentFilter. addAction ("android. intent. action. HEADSET_PLUG ");
RegisterReceiver (headsetplugreceivreceiver, intentFilter );
}
@ Override
Public void onDestroy (){
UnregisterReceiver (headsetplugreceivreceiver );
Super. onDestroy ();
}
}

In this way, the earphones can be inserted and pulled out.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.