views:

699

answers:

1

Hi,

I want to handle key press and long key press for the key code KEYCODE_BACK(back button). can any one suggest me how to do this in android 1.5(API level 3).

Here is the code.

public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {

                if(event.getRepeatCount()==0) {
                   // normal key press
                   //Do something here
                   // But problem is, this code is hitting for long press also, how to avoid this
                } else {
                    // Long key press
                    //Do something here
                }
                // Always consume back key event by returning true
                //so that default behavior of back is overrided
                return true;
            }
        return super.onKeyDown(keyCode, event);
    }

The problem is the code for normal key press is also executed on long press. I want to avoid this.

Note: I can't use methods like onKeyLongPress() , startTracking() etc as they are introduced in API level 5

A: 

The actual event dispatching code doesn't have a concept of long presses and all you need to do is timing for them on the main thread of the application, the application might get slower enough so as not to update within the long press timeout.

This is because, there's no a real Keypress event handling present with the android 1.5. This one got introduced with Android 2.0 a real KeyEvent API and callback functions for long presses.

Vishwanath