Android detects the insertion and unplugging status of headphones and android headsets.

Source: Internet
Author: User

Android detects the insertion and unplugging status of headphones and android headsets.

In Android, A BroadcastReceiver must be created to monitor the broadcast of "android. intent. action. HEADSET_PLUG.


Steps:

1. Create a subclass of BroadcastReceiver, override the onReceive () method, and write the processing logic after receiving the broadcast in this method;

2. Create an Activity class and use the registerReceiver () method in the onCreate () method to register and listen for broadcast;

3. Rewrite the onDestory () method in the Activity and use unregisterReceiver () to cancel the listening broadcast.

The specific implementation code is as follows:

Broadcast receiver class

Package com. leigo. demo. explorer; import android. content. broadcastReceiver; import android. content. context; import android. content. intent; import android. widget. toast;/*** Created by Administrator on 2014/8/27. */public class HeadsetDetectReceiver extends BroadcastReceiver {@ Override public void onReceive (Context context, Intent intent) {String action = intent. getAction (); if (Intent. ACTION_HEADSET_PLUG.equals (action) {if (intent. hasExtra ("state") {int state = intent. getIntExtra ("state", 0); if (state = 1) {Toast. makeText (context, "insert headset", Toast. LENGTH_SHORT ). show ();} else if (state = 0) {Toast. makeText (context, "unplug the headset", Toast. LENGTH_SHORT ). show ();}}}}}


package com.leigo.demo;import android.app.Activity;import android.content.BroadcastReceiver;import android.content.Intent;import android.content.IntentFilter;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import com.leigo.demo.receiver.HeadsetDetectReceiver;public class MainActivity extends Activity {    private BroadcastReceiver receiver;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        /**         *  Broadcast Action: Wired Headset plugged in or unplugged         *  The intent will have the following extra values:         *  state - 0 for unplugged, 1 for plugged.         *  name - Headset type, human readable string         *  microphone - 1 if headset has a microphone, 0 otherwise         */        receiver = new HeadsetDetectReceiver();        IntentFilter intentFilter = new IntentFilter();        intentFilter.addAction(Intent.ACTION_HEADSET_PLUG);        registerReceiver(receiver, intentFilter);    }    @Override    protected void onDestroy() {        super.onDestroy();        unregisterReceiver(receiver);    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();        if (id == R.id.action_settings) {            return true;        }        return super.onOptionsItemSelected(item);    }}


Android detects whether a user has a headset

<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. In Android, the system detects the insertion and unplugging of headphones, that is, creating a Broadcast Receiver to listen to "android. intent. action. HEADSET_PLUG "broadcast but directly in AndroidManifest. adding a <Cycler> label in xml is invalid, for example, <Cycler android: name = ". headsetplugreceivreceiver "> <intent-filter> <action android: name =" android. intent. action. HEADSET_PLUG "android: enabled =" true "> </action> </intent-filter> </receiver> 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 monitor the insertion and removal of headphones: public class HeadsetPlugReceiver extends BroadcastReceiver {private static final String TAG = "headsetplugreceivreceiver"; @ Override public void onReceive (Context 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 () in the Activity that needs to listen to the event, and do not forget to log off listening to the broadcast in onDestroy: 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. la ...... remaining full text>

Why does my cell phone headset show the "plugged in headset" status?

There is a problem with the headset plug-in on the mobile phone. When the headset is inserted into the mobile phone, the Reed in the headset plug-in is pushed up, and the mobile phone will judge that the headset is inserted and displayed.
"Insert a headset". On the contrary, when the headset is pulled out, the reed is restored, and the mobile phone determines that the headset is pulled out without display. Your mobile phone is unrecovered because the reed is not recovered, causing a false positive judgment on the mobile phone. You can insert the headset into your phone and pull it out. Try it several times to see if it can be restored. If you cannot, change the outlet after sales.

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.