Android + earphone plugging and earphone button Detection

Source: Internet
Author: User

1. earphone plugging

First, register the aggreger in oncreate,

 registerReceiver(headSetReceiver, new IntentFilter(Intent.ACTION_HEADSET_PLUG)); 

Next, it is the receiving broadcast part:

Private Final broadcastreceiver headsetreceiver = new broadcastreceiver () {@ override public void onreceive (context, intent) {log. D (TAG, "into headsetreceiver! "); String action = intent. getaction (); If (action. equals (intent. action_headset_plug) {// headphone plugged if (intent. getintextra ("State", 0) = 1) {// do something log. D (TAG, "this is headphone plugged"); headsetstate. settext ("headset Detection: INSERTED"); // headphone unplugged} else {// do something log. D (TAG, "this is headphone unplugged"); headsetstate. settext ("earphone Detection: No insertion ");}}}};

 

2. earphone button Detection

/* Earphone buttons */Public Boolean onkeydown (INT keycode, keyevent event) {Switch (keycode) {Case keyevent. keycode_headsethook: // displaytoast ("Press: headset key"); headsetstate. settext ("headset Detection: insert, press the headset button"); break;} return true;} public Boolean onkeyup (INT keycode, keyevent event) {Switch (keycode) {Case keyevent. keycode_headsethook: // displaytoast ("pop-up: headset key"); headsetstate. settext ("headset Detection: insertion, headset button bounce"); break;} return true ;}

The headset buttons can be detected. Some people on the Internet say that the following method can be used to detect the headset buttons, and there are more than N reprinted. I tried... Hope that a successful buddy will give me a message after seeing it. Communication is very important...

You only need to add intent. action_media_button to the filter and then process final keyevent event = (keyevent) intent. getparcelableextra (intent. extra_key_event) in the onreceive function; If (event! = NULL & event. getaction () = keyevent. action_down) {// do something}

As mentioned above, I now understand that the concept of a priority for broadcasting and receiving is in it, and the hook is intercepted by the system's own broadcaster, after the application is used up, it is destroyed again. As a result, my application cannot listen to the hook button. As long as the priority of the Broadcast Reception is increased, the system will be 1000. My number is 1000 + 1. OK!

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.