Mar 30, 2011

Detect Key action

To detect key action of Android device from user, you can override the methods onKeyDown() and onKeyUp().

Example:
package com.AndroidKey;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.Toast;

public class AndroidKey extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub

switch(keyCode){
case KeyEvent.KEYCODE_MENU:
Toast.makeText(AndroidKey.this, "KEYCODE_MENU", Toast.LENGTH_LONG).show();
return true;
case KeyEvent.KEYCODE_SEARCH:
Toast.makeText(AndroidKey.this, "KEYCODE_SEARCH", Toast.LENGTH_LONG).show();
return true;
//...more case...
default:
return super.onKeyDown(keyCode, event);
}
}
}


No comments:

Post a Comment

Infolinks In Text Ads