Question

i'm trying to detect (via BroadcastReciever) hard pressing on original samsung headset buttons.

the code is simple:

public class MediaButtonIntentReceiver extends BroadcastReceiver {

public MediaButtonIntentReceiver() {
    super();
}

@Override
public void onReceive(Context context, Intent intent) {
    String intentAction = intent.getAction();
    Toast.makeText(context, "Action: "+intentAction, Toast.LENGTH_SHORT).show(); 
    if (!Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
        return;
    }
    Toast.makeText(context, "BUTTON PRESSED!", Toast.LENGTH_SHORT).show(); 
    KeyEvent event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
    if (event == null) {
        return;
    }
    int action = event.getAction();
    if (action == KeyEvent.ACTION_DOWN) {
    // do something
        Toast.makeText(context, "BUTTON PRESSED!", Toast.LENGTH_SHORT).show(); 
    }
    abortBroadcast();
}

Manifest:

 <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver android:name=".MediaButtonIntentReceiver" >
        <intent-filter android:priority="100000000000000" >
            <action android:name="android.intent.action.MEDIA_BUTTON" />
        </intent-filter>
    </receiver>
</application>

I've tested it in galaxy s1 (2 rooted devices, nothing happened) and galaxy NOTE (media was started/stopped and volume has been changed accordignly, but nothing happened from the code ..), in HTC everything worked fine. I'm 99% sure it's not priority problem as I changed it several times.

I would appreciate if someone knows what's going on in there. Thanks. Udi

Was it helpful?

Solution

You also need to call registerMediaButtonEventReceiver on AudioManager in order to receive the events. That state will hold until something else calls registerMediaButtonEventReceiver() or until you call unregisterMediaButtonEventReceiver().

For example, an activity like this:

public class MediaButtonActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ((AudioManager)getSystemService(AUDIO_SERVICE)).registerMediaButtonEventReceiver(new ComponentName(
                                                                                                   this,
                                                                                                   MediaButtonIntentReceiver.class));
    }
}

will enable your manifest-registered MediaButtonIntentReceiver to get ACTION_MEDIA_BUTTON events.

OTHER TIPS

Try Turning off S-Voice and running your application again .

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top