问题
I want my app to do some specific actions when user long clicks (press and hold) headset button. I use a BroadcastReceiver
for MEDIA_BUTTON
and it works fine in phones who doesn't run Google now app for sound actions for long press. But in the phones that automatically run Google now my app is just being ignored. How can I disable Google now and detect headset button long click. here is onReceive
method in my BroadcastReceiver
class.
public void onReceive(Context context, Intent intent) {
String intentAction = intent.getAction();
if (!Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
return;
}
KeyEvent event = (KeyEvent) intent
.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if (event == null) {
return;
}
int action = event.getAction();
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_HEADSETHOOK:
/**
* for long click
*/
if (action == KeyEvent.ACTION_DOWN) {
if (!isDown) {
isDown = true;
handler.postDelayed(l, LONG_CLICK_DELAY);
}
}
if (action == KeyEvent.ACTION_UP) {
/**
* for long click
*/
if (isDown)
isDown = false;
}
break;
}
abortBroadcast();
}
回答1:
I believe that your efforts may be in vain due to this Android bug.
I assume that will override anything you attempt, or at best, start your app as well as Google Now....
I've only managed to get around this on rooted devices whilst I wait for Google to fix it sigh
回答2:
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.
回答3:
You can override onKeyDown and look for the KeyEvent.KEYCODE_HEADSETHOOK key to handle the case when your app is in the foreground.
As to the background, ordered broadcasts can be aborted by a receiver which will stop any other app from receiving the broadcast and I'm guessing that is what Google Play is doing. The only way to combat that is to set android:priority
in your manifest to a suitably high value to come before Google Play and hope Google Play developers didn't choose Integer.MAX_VALUE
.
来源:https://stackoverflow.com/questions/20991248/google-now-wont-let-my-app-detect-headset-button-long-click