In Android how do you register to receive headset plug broadcasts?

后端 未结 3 932
无人及你
无人及你 2020-12-10 09:10

I am working in Android 2.1, and I want to detect when the headset is plugged in/taken out. I\'m pretty new to android.

I think the way to do it is using a Broadcast

相关标签:
3条回答
  • 2020-12-10 09:39

    Here are two sites that may help explain it in more detail:

    • http://www.grokkingandroid.com/android-tutorial-broadcastreceiver/
    • http://www.vogella.com/articles/AndroidBroadcastReceiver/article.html

    You have to define your intent; otherwise it won't access the system function. The broadcast receiver; will alert your application of changes that you'd like to listen for.

    Every receiver needs to be subclassed; it must include a onReceive(). To implement the onReceive() you'll need to create a method that will include two items: Context & Intent.

    More then likely a service would be ideal; but you'll create a service and define your context through it. In the context; you'll define your intent.

    An example:

    context.startService
          (new Intent(context, YourService.class));
    

    Very basic example. However; your particular goal is to utilize a system-wide broadcast. You want your application to be notified of Intent.ACTION_HEADSET_PLUG.

    How to subscribe through manifest:

    <receiver
        android:name="AudioJackReceiver"
        android:enabled="true"
        android:exported="true" >
        <intent-filter>
            <action android:name="android.intent.action.HEADSET_PLUG" />
        </intent-filter>
    </receiver>
    

    Or you can simply define through your application; but. Your particular request; will require user permissions if you intend to detect Bluetooth MODIFY_AUDIO_SETTINGS.

    0 讨论(0)
  • 2020-12-10 09:46

    You need to enable the broadcast receiver and set the exported attribute to true:

    <receiver
        android:name="AudioJackReceiver"
        android:enabled="true"
        android:exported="true" >
        <intent-filter>
            <action android:name="android.intent.action.HEADSET_PLUG" />
        </intent-filter>
    </receiver>
    
    0 讨论(0)
  • 2020-12-10 09:52

    Just complementing Greg`s answer, here is the code that you need divided in two parts

    1. Register the Service in the first Activity (here its called MainActivity.java).

    2. Switch over the result of the ACTION_HEADSET_PLUG action in the BroadCastReceiver.

    Here it goes:

    public class MainActivity extends Activity  {
    private static final String TAG = "MainActivity";
    private MusicIntentReceiver myReceiver;
    
    @Override protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myReceiver = new MusicIntentReceiver();
    }
    
    @Override public void onResume() {
        IntentFilter filter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
        registerReceiver(myReceiver, filter);
        super.onResume();
    }
    
    private class MusicIntentReceiver extends BroadcastReceiver {
        @Override public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
                int state = intent.getIntExtra("state", -1);
                switch (state) {
                case 0:
                    Log.d(TAG, "Headset is unplugged");
                    break;
                case 1:
                    Log.d(TAG, "Headset is plugged");
                    break;
                default:
                    Log.d(TAG, "I have no idea what the headset state is");
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题