Key events executed on key-up

Android 2.0 is designed to run on devices that use virtual keys for HOME, MENU, BACK, and SEARCH, rather than physical keys. To support the best user experience on those devices, the Android platform now executes these buttons at key-up, for a key-down/key-up pair, rather than key-down. This helps prevent accidental button events and lets the user press the button area and then drag out of it without generating an event.

This change in behavior should only affect your application if it is intercepting button events and taking an action on key-down, rather than on key-up. Especially if your application is intercepting the BACK key, you should make sure that your application is handling the key events properly.

In general, intercepting the BACK key in an application is not recommended, however, if your application is doing so and it invokes some action on key-down, rather than key-up, you should modify your code.

If your application will use APIs introduced in Android 2.0 (API Level 5), you can take advantage of new APIs for managing key-event pairs:

  • If you are intercepting the BACK key in an activity or dialog, just implement the new onBackPressed() method.
  • If you are intercepting the BACK key in a view, you should track the key event on key-down (through the new startTracking() method), then invoke the action at key up. Here's a pattern you can use:

http://docs.androidside.com/docs/sdk/android-2.0.html
        public boolean onKeyDown(int keyCode, KeyEvent event) {
           
    if (keyCode == KeyEvent.KEYCODE_BACK
                   
    && event.getRepeatCount() == 0) {
               
    event.startTracking();
               
    return true;
           
    }
           
    return super.onKeyDown(keyCode, event);
       
    }

       
    public boolean onKeyUp(int keyCode, KeyEvent event) {
           
    if (keyCode == KeyEvent.KEYCODE_BACK && event.isTracking()
                   
    && !event.isCanceled()) {
               
    // *** DO ACTION HERE ***
               
    return true;
           
    }
           
    return super.onKeyUp(keyCode, event);
       
    }

If you want to update a legacy application so that its handling of the BACK key works properly for both Android 2.0 and older platform versions, you can use an approach similar to that shown above. Your code can catch the target button event on key-down, set a flag to track the key event, and then also catch the event on key-up, executing the desired action if the tracking flag is set. You'll also want to watch for focus changes and clear the tracking flag when gaining/losing focus.

Android 에서 key, touch, trackball 같은 event의 경우에는 WindowManagerService라는 서비스에서 Event Dispatch에 의해서 관리가 됩니다.
Event Dispatch 경로를 간단히 나타내면 아래와 같습니다.
 
문제는 이 WindowManagerService에서 이벤트를 Dispatch할 때
Home Key와 Power(Endcall) Key의 경우에는 Event를 받을 View에 Dispatch하기 전에
먼저 HomeKey와 Power(Endcall)Key의 작업을 수행한 후 Dispatch시키지 않는 다는 것입니다.
 
이는 안드로이드 키이벤트 정책 상의 이유로
PhoneWindowManager의 interceptKeyTi(HomeKey관련) interceptKeyTq(Power,Endcall Key관련)함수에
해당 작업이 정의되어 있습니다.
 
실제로 이 두 함수를 수정하면 Event를 해당 View까지 Dispatch 시킬 수 있지만…
권장되는 사항은 아닙니다.
 
이와 같은 이유로 HomeKey와 Power(Endcall)Key는 keydown,up 과 같은 함수로 인식 할 수 없는 것입니다.

출처 : http://blog.naver.com/realwind/20119124672 

'안드로이드' 카테고리의 다른 글

안드로이드 부팅 순서  (0) 2011.05.16
Key events executed on key-up  (0) 2011.05.12
Thread 우선순위 설정.  (0) 2011.05.03
Looper  (0) 2011.05.03
DIP를 px로 변환하기  (0) 2011.04.21

public class SchedulingTest4 {

    public static void main(String args[]) {
        Thread[] t = new RunThread4[3];
    
        t[0] = new RunThread4("☆");
        t[0].start(); 
        t[0].setPriority(1);  // ☆ 가장 느린 우선 순위
        
        t[1] = new RunThread4("◑");        
        t[1].start();
        t[1].setPriority(5);

        t[2] = new RunThread4("○");        
        t[2].start();        
        t[2].setPriority(10); // ○ 가장 빠른 우선 순위
        /*
        System.out.println("t[0]" + t[0].getPriority());
        System.out.println("t[1]" + t[1].getPriority());
        System.out.println("t[2]" + t[2].getPriority());
        */        
    } 
}

+ Recent posts