Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

[Android]BroadcastReceiver(Action_headset_plug) not firing

Tags:

android

I declared a BroadcsastReceiver(Action_headset_plug) in AndroidManifest.xml and defined a BroadcastHandler.class implement BroadcsastReceiver . I run the apk on the device and the receiver doesn't fire. However , it work correctly when I use registerReceiver() in the Activity. Do I miss something in the AndroidManifest.xml?

This is the AndroidManifest.xml

  <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="irdc.Earphone_test"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="9" />
<uses-permission android:name="android.permission.ACTION_HEADSET_PLUG"></uses-permission>
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <receiver  android:enabled="true" android:name="BroadcastHandler">
            <intent-filter>
                <action android:name="android.intent.ACTION_HEADSET_PLUG"></action>
            </intent-filter>
        </receiver>
        <activity android:name=".Earphone_testActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

This is the receiver code

public class BroadcastHandler extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) {    

        if(intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)){                    
            String mes;
            int state = intent.getIntExtra("state", 4);
            if(state == 0){    
                mes ="out";   
            }else if(state == 1){    
                mes ="in";           
            }else {         
                mes ="others";
            }
            AlertDialog.Builder builder = new AlertDialog.Builder(context);        
            builder.setTitle("Headset broadcast");       
            builder.setMessage(mes);      
            builder.setPositiveButton("Okey-dokey", new DialogInterface.OnClickListener() {    
                public void onClick(DialogInterface dialog, int which) {              
                    dialog.dismiss();          
                }      
            });       
            builder.create().show();

        } 

    } 
}
like image 906
neo0093 Avatar asked Sep 20 '11 06:09

neo0093


2 Answers

For listening to headset changes, the broadcast receiver cannot be declared in the manifest, it must be dynamically registered. Not all receivers work when declared in the manifest and this is an example where you need to register it programmatically.

You can call this for example in an Activity's onResume method or in a Service's onCreate method:

headsetReceiver = new BroadcastHandler();
registerReceiver(headsetReceiver, new IntentFilter(Intent.ACTION_HEADSET_PLUG));

Then in the Activity's onPause method or in the Service's onDestroy method you need to unregister the receiver:

unregisterReceiver(headsetReceiver);
like image 73
nunof Avatar answered Sep 27 '22 20:09

nunof


The name is wrong in the manifest entry. Use the full package name, or start it with a period if you want it implicitly appended to the app's package name:

<receiver  android:enabled="true" android:name=".BroadcastHandler">
like image 33
Mike Avatar answered Sep 27 '22 20:09

Mike