[Android] BroadcastReceiver (Action_headset_plug) not working

I declared BroadcsastReceiver (Action_headset_plug) in AndroidManifest.xml and defined BroadcastHandler.class to implement BroadcsastReceiver. I run apk on the device and the receiver does not work. However, it works correctly when I use registerReceiver () in Activity. Am I missing something in AndroidManifest.xml?

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

        } 

    } 
}
+5
source share
3 answers

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

, , Activity onResume Service onCreate:

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

Activity onPause Service onDestroy :

unregisterReceiver(headsetReceiver);
+5

. , , :

<receiver  android:enabled="true" android:name=".BroadcastHandler">
+1

AlertDialog, Toast . , Activity, "content" , , AlertDialog. ( )

, Toast, , .

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

0

All Articles