I'm developing a headset button controller and use a broadcast receiver for detecting headset button presses.
((AudioManager) getSystemService(AUDIO_SERVICE)).registerMediaButtonEventReceiver(new ComponentName(getPackageName(), MediaButtonIntentReceiver.class.getName()));
onReceive
method:
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:
if (action == KeyEvent.ACTION_DOWN) {
long time = SystemClock.uptimeMillis();
// double click
if (time - sLastClickTime < DOUBLE_CLICK_DELAY)
// do something
Toast.makeText(context, "BUTTON PRESSED DOUBLE!",
Toast.LENGTH_SHORT).show();
// single click
else {
// do something
Toast.makeText(context, "BUTTON PRESSED!",
Toast.LENGTH_SHORT).show();
}
sLastClickTime = time;
}
break;
}
abortBroadcast();
}
and it works fine. The problem is in HTC android phones It can't receive double click. When you double click headset button it dials your last call by default and I can't detect double click in my app. Is there any way to disable this action with android APIs?
I tried setting my broadcast receiver's priority to a large number and it didn't work. Even I tried to placing a fake/invalid call in phone's call log but I couldn't do that. Any Idea how can I solve this problem?
The solution was so simple. Setting priority to largest integer number (2147483647) instead of largest value defined by Google (1000), in manifest file solves the problem, and phone doesn't dial last number by double clicking and broadcast receiver detects the headset button double click.
<receiver
android:name=".MediaButtonIntentReceiver"
android:enabled="true" >
<intent-filter android:priority="2147483647">
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>