안드로이드 버젼 2.0부터 HOME, MENU, BACK, SEARCH 키가 가상키로 지원하는데, 일반적으로
onKeyUp에서 해당 버튼들을 실행 시키는 것이 좋다.
-다이얼로그(Dialog)나 액티비티(Activity)에서는 onBackPressed()로 구현
-뷰에서 처리할 때는 onKeyDown 내에서 새롭게 정의한 StartTracking() 로 키 이벤트를
처리하고, 추가작업은 onKeyUp로 다음과 같이 처리함.
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 ***//
retrun true;
}
return super.onKeyUp(keyCode, event);
}
================================================================
[안드로이드용 권고 소스]
public boolean onKeyDown(int keyCode, KeyEvent event){
if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.ECLAIR && keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
//Take care of calling this method on earlier versions of
//the platform where it doesn't exist.
onBackPressed();
}
return.super.onKeyDown(keyCode, event);
}
public void onBackPressed(){
return;
}
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 ***//
retrun true;
}
return super.onKeyUp(keyCode, event);
}
================================================================
[안드로이드용 권고 소스]
public boolean onKeyDown(int keyCode, KeyEvent event){
if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.ECLAIR && keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
//Take care of calling this method on earlier versions of
//the platform where it doesn't exist.
onBackPressed();
}
return.super.onKeyDown(keyCode, event);
}
public void onBackPressed(){
return;
}