Hello everyone…

In this post I will show you how to listen to hardware button in an android smartphone.

This post especially shows how to listen to the home button in android. But my advice is never override the home button in your app.
Home button is to move the currently running process to background. But you can override the Backbutton and other buttons, android allows that on the keyDown Method. But if you try to match the “Home” button in the keyDown method it will not work. For that we have to follow another way which is shown in this post.

This simple java code handles the button pressed.

package test.test;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.WindowManager;

public class Test2Activity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_HOME)) {
        	System.out.println("KEYCODE_HOME");
        	showDialog("'HOME'");
            return true;
        }
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
        	System.out.println("KEYCODE_BACK");
        	showDialog("'BACK'");
            return true;
        }
        if ((keyCode == KeyEvent.KEYCODE_MENU)) {
        	System.out.println("KEYCODE_MENU");
        	showDialog("'MENU'");
            return true;
        }
        return false;
    }
    
    void showDialog(String the_key){
    	AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	builder.setMessage("You have pressed the " + the_key + " button. Would you like to exit the app?")
    		  .setCancelable(true)
    	       .setPositiveButton("OK", new DialogInterface.OnClickListener() {
    	           public void onClick(DialogInterface dialog, int id) {
    	        	   dialog.cancel();
    	        	   finish();
    	           }
    	       })
    	       .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    	           public void onClick(DialogInterface dialog, int id) {
    	                dialog.cancel();
    	           }
    	       });
    	AlertDialog alert = builder.create();
    	alert.setTitle("CoderzHeaven.");
    	alert.show();
    }
    
    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);           
    }
    
    public void onUserLeaveHint() { // this only executes when Home is selected.
    	// do stuff
    	super.onUserLeaveHint();
    	System.out.println("HOMEEEEEEEEE");
    }
}

When “onUserLeaveHint()” function is defined you can actually listen to the home button press and do some saving of data or something.

 public void onUserLeaveHint() { // this only executes when Home is selected.
    	// do stuff
    	super.onUserLeaveHint();
    	System.out.println("HOMEEEEEEEEE");
    }

Home button override

Home button override

Home button override

Please leave your comments on this post and share it on the social Networks.

 

5 Comments

  1. ali ahmad August 14, 2013 at 9:50 am

    its not working on jelly bean

     
    • James August 15, 2013 at 1:27 pm

      May be google has removed it in the latest versions. However we shouldnot override the HomeButton.

       
      • Anita Gomber January 20, 2014 at 11:34 am

        No doubt Its working fine with 2.3.3 but not working with jelly bean. If you find something with jelly bean kindly post on the website.

         
        • James January 31, 2014 at 6:18 pm

          May be for security reasons Google has removed it.

           
    • Anita Gomber January 20, 2014 at 11:35 am

      May be u r right.

       

Leave a Reply

Your email address will not be published. Required fields are marked *

 

 

 

Theme by HermesThemes

Copyright © 2018 CoderzHeaven. All Rights Reserved

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.