[Android development FAQ-5] How to check the headset insertion status

Source: Internet
Author: User

Recently, I have been studying how to use headphones for some control operations, which naturally involves how to check whether the earphones are inserted. After some queries and experiments, I will summarize the following:

1. Principle:

In fact, the android system will send broadcasts when the earphones are inserted and pulled out. Therefore, to detect the status of the earphones, you only need to register the broadcastreceiver to respond to the status.

The broadcast name is:Android. Intent. Action. headset_plug

Note: add the response permission in manifest. xml:

<Uses-Permission Android: Name = "android. Permission. modify_audio_settings"/>

Note: broadcast must be dynamically registered in the Java file.


2. Code

/*** Created by: Alex */package COM. alex. erji; import android. app. activity; import android. content. broadcastreceiver; import android. content. context; import android. content. intent; import android. content. intentfilter; import android. OS. bundle; import android. widget. toast; public class mainactivity extends activity {@ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); // intentfilter bound to the broadcast response intentfilter = new intentfilter (); intentfilter. addaction ("android. intent. action. headset_plug "); headsetreceiver = new headsetreceiver (); registerreceiver (headsetreceiver, intentfilter );} // custom broadcast receiver public class headsetreceiver extends broadcastreceiver {@ overridepublic void onreceive (context, intent) {If (intent. hasextra ("state") {If (0 = intent. getintextra ("State", 0) {toast. maketext (context, "headset not inserted", toast. length_short ). show ();} else if (1 = intent. getintextra ("State", 0) {toast. maketext (context, "headset inserted", toast. length_short ). show ();}}}}}

After normal operation, the system will remind you that the headset is inserted. When you unplug the headset, the system will remind you that the headset is not inserted ".

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.