5

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();

        } 

    } 
}
neo0093
  • 51
  • 1
  • 2

3 Answers3

5

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);
nunof
  • 227
  • 2
  • 8
  • 1
    Why is this one different from the other ones? Do you have a link to documentation that confirms this? – shim Nov 11 '12 at 00:08
  • http://developer.android.com/reference/android/content/Intent.html makes a note for ones that cannot be registered through the app manifest, and this is not one of them. – shim Nov 11 '12 at 01:12
  • However, I was able to get it to work with this method. Thank you! – shim Nov 11 '12 at 01:21
1

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">
Mike
  • 2,422
  • 23
  • 14
0

Most examples of receivers do not start an AlertDialog but Toast a message or create a Notification in Status bar. I am sure that you cannot start an Activity because the "content" object stops existing but not if you can built an AlertDialog. (documentation of Broadcasts)

So you can try at your receiver a Toast message to make sure that it works.

Example with Notification: http://www.anddev.org/recognize-react_on_incoming_sms-t295.html

madlymad
  • 6,367
  • 6
  • 37
  • 68
  • ["With respect to your error, a manifest-registered BroadcastReceiver is not an Activity, and so it cannot create a dialog."](http://stackoverflow.com/questions/3432601/alertdialog-in-broadcastreceiver/3432645#3432645) – madlymad Sep 20 '11 at 07:26