Sunday, 13 July 2014

Managing Bottom key like HOME,MENU,BACK keys in Android

Hello Friends,

                  Once again I am back with the article which will guide for managing android device's Basic keys i.e. HOME,MENU,BACK,SETTINGS,SEARCH keys which comes common in most of the devices. And sometimes its needed to manage this buttons click events in our application.

There are various Android Application where we need to control the android bottom key panel. This bottom key panel is organised by five keys

1. Back
2. Menu
3. Home
4. Search
5. Settings

If we take any example like online payment Application then we can’t allow a user to press the back button in the middle of payment. So we have to override the back press event and managed the click event.

There are many cases where we provide certain action of the application like action bar menu will be appeared when menu button user will pressed.
This key panel button events are identified by android in following ways
1. Back – KEYCODE_BACK
2. Menu -KEYCODE_MENU
3. Home – KEYCODE_HOME
4. Search – KEYCODE_SEARCH
5. Settings – KEYCODE_SETTINGS
For handling this keys we need to override this method of the Activity
 @Override  
 public boolean onKeyDown(int keyCode, KeyEvent event) {  
 if (keyCode == KeyEvent.KEYCODE_BACK) {  
 //Do Code Here  
 // If want to block just return false  
 return false;  
 }  
 if (keyCode == KeyEvent.KEYCODE_MENU) {  
 //Do Code Here  
 // If want to block just return false  
 return false;  
 }  
 if (keyCode == KeyEvent.KEYCODE_HOME) {  
 //Do Code Here  
 // If want to block just return false  
 return false;  
 }  
 if (keyCode == KeyEvent.KEYCODE_SEARCH) {  
 //Do Code Here  
 // If want to block just return false  
 return false;  
 }  
 if (keyCode == KeyEvent.KEYCODE_SETTINGS) {  
 //Do Code Here  
 // If want to block just return false  
 return false;  
 }  
 return super.onKeyDown(keyCode, event);  
 }  

From Android 4.0 (ICS) onward KEYCODE_HOME has been deprecated. As Android believed that it is wrong to give all the option blocked for user. User can pressed the Home button to navigate to other application. But android keep the current application as it is state in the background. So that user can back to the previous application.
Below i am showing code to manage HOME_KEY in android.
 public class DisableHardButton extends Activity {  
  TextView mTextView;  
  ToggleButton mToggleButton;  
  boolean isLock=false;  
  @Override  
  public void onCreate(Bundle savedInstanceState) {  
  super.onCreate(savedInstanceState);  
  setContentView(R.layout.main);  
  mTextView=(TextView) findViewById(R.id.tvInfo);  
  mToggleButton=(ToggleButton) findViewById(R.id.btnLock);  
  mToggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {  
   @Override  
   public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {  
   isLock=isChecked;  
   onAttachedToWindow();  
   }  
  });  
  }  
  @Override  
  public boolean dispatchKeyEvent(KeyEvent event) {  
  if ( (event.getKeyCode() == KeyEvent.KEYCODE_HOME) && isLock) {  
   mTextView.setText("KEYCODE_HOME");  
   return true;  
  } else {  
   return super.dispatchKeyEvent(event);  
  }  
  }  
  @Override  
  public boolean onKeyDown(int keyCode, KeyEvent event) {  
  if( (keyCode==KeyEvent.KEYCODE_BACK) && isLock) {  
   mTextView.setText("KEYCODE_BACK");  
   return true;  
  }else {  
   return super.onKeyDown(keyCode, event);  
  }  
  }  
  @Override  
  public void onAttachedToWindow() {   
  System.out.println("Onactivity attached :"+isLock);  
  if(isLock) {    
   this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);     
   super.onAttachedToWindow();  
  }else {  
   this.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION);     
   super.onAttachedToWindow();  
  }  
  }  
 }  

This is layout file main.xml 
 <?xml version="1.0" encoding="utf-8"?>  
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:layout_width="fill_parent"  
   android:layout_height="fill_parent"  
   android:orientation="vertical" >  
   <TextView  
     android:id="@+id/tvInfo"  
     android:layout_width="fill_parent"  
     android:layout_height="wrap_content"  
     android:text="Hi! This is the testing of override home button" />  
   <ToggleButton  
     android:id="@+id/btnLock"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:textOff="UnLocked"  
     android:textOn="Locked" />  
 </LinearLayout>  

There are another instance user can accidentally press back button and exit the application. In that case we have to give alert to the user before quit the application. For that we need to identify for the last activity in the stack. For that we need to identify and checked for the last activity form the stack.
 private boolean isLastActivity() {  
     final ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);  
     final List tasksInfo = am.getRunningTasks(1024);  
     final String ourAppPackageName = getPackageName();  
     RunningTaskInfo taskInfo;  
    final int size = tasksInfo.size();  
     for (int i = 0; i < size; i++) {  
        taskInfo = tasksInfo.get(i);  
      if (ourAppPackageName  
 .equals(taskInfo.baseActivity.getPackageName())) {  
     return taskInfo.numActivities == 1;  
     }  
    }  
   return false;  
 }  

This way we can manage the android key events 

No comments:

Post a Comment