简体   繁体   中英

Detect button press in samsung headsets Android

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

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.

尝试关闭S-Voice并再次运行您的应用程序。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM