KKO
KKO

Reputation: 1933

Click-and-hold the headset button shows no action

I have a BroadcastReceiver and my goal is to detect a long press on the headset button. Tried different versions and none worked. So now I'm trying to check the actions received. If I click normally, I can see the actions 0 (KeyDown) and 1 (KeyUp). If I click and hold, nothing shows up, for either key down or up. What's wrong?

@Override
public void onReceive(Context context, Intent intent) {
    if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {
        KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
        Log.e("action",""+event.getAction());
        ...
    }
}

I can post more code if required, but I didn't think it was relevant

Running on an S4 with Android 4.3. Not sure if it matters, S-Voice is frozen, Google Now is disabled. And I've noticed that if my screen is off and I long click, I get a menu "Handle long press with Google Search / AutoVoice"

Upvotes: 2

Views: 514

Answers (1)

engico
engico

Reputation: 453

I think you can take control over long press with this bit of code in manifest. This will open up a dialog which lets you choose which app to use long press. It worked for me when phone is unlocked but it wont work when the phone is locked. It pops out dialog but it vanishes when you unlock the phone. Hope I can get some help with this.

Key is to open up "Activity" instead of implementing broadcast receiver.

   <activity 
            android:name="packagename.activityname"
              android:launchMode="singleInstance"
               android:showOnLockScreen="true">
             <intent-filter android:priority="2147483647">
                <action android:name="android.speech.action.WEB_SEARCH" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            <intent-filter android:priority="2147483647">
                <action android:name="android.speech.action.VOICE_SEARCH_HANDS_FREE" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            <intent-filter>
                <action android:name="com.sec.action.SVOICE" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

I have tested this on Kitkat. No idea what happens on previous versions.

Upvotes: 1

Related Questions