How to override hardware Home button in android? OR How to listen to home button click in android?

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.

WaterProof, DustProof and scratch resistant Smartphones arriving in the market – Xperia Go and Xperia Acro S

Sony has introduced two new durable and waterproof handsets. For once, rugged doesn’t equate with bad design. The two Android-powered handsets are svelte and streamlined but feature extra protection like scratch-resistant displays and wet finger tracking.

Sony announces Xperia S, Xeria P, Xperia U and Xperia Sola in India
Sony Xperia U goes on sale online
scratch-resistant displays and wet finger tracking.

Xperia Go Specifications

Xperia Go

GENERAL 2G Network GSM 850 / 900 / 1800 / 1900
3G Network HSDPA 900 / 2100 – ST27i
HSDPA 850 / 1900 / 2100 – ST27a
Announced 2012, May
Status Coming soon. Exp. release 2012, Q3
BODY Dimensions 111 x 60.3 x 9.8 mm
Weight 110 g
- Touch-sensitive controls
DISPLAY Type LED-backlit LCD, capacitive touchscreen, 16M colors
Size 320 x 480 pixels, 3.5 inches (~165 ppi pixel density)
Multitouch Yes, up to 4 fingers
Protection Scratch-resistant glass
- Sony Mobile BRAVIA Engine
- IP67 certified – dust and water proof (up to 1m for 30 mins)
SOUND Alert types Vibration; MP3 ringtones
Loudspeaker Yes
3.5mm jack Yes
MEMORY Card slot microSD, up to 32 GB
Internal 8 GB storage (4 GB user available), 512 MB RAM
DATA GPRS Up to 107 kbps
EDGE Up to 296 kbps
Speed HSDPA, 14.4 Mbps; HSUPA, 5.76 Mbps
WLAN Wi-Fi 802.11 b/g/n, Wi-Fi Direct, DLNA, Wi-Fi hotspot
Bluetooth Yes, v3.0 with A2DP, EDR
USB Yes, microUSB v2.0, USB On-the-go support
CAMERA Primary 5 MP, 2592 x 1944 pixels, autofocus, LED flash
Features Geo-tagging, touch focus, face and smile detection, 3D sweep panorama
Video Yes, 720p@30fps, continuous autofocus, video light, video stabilizer
Secondary No
FEATURES OS Android OS, v2.3 (Gingerbread), planned upgrade to v4.0
Chipset NovaThor U8500
CPU Dual-core 1 GHz Cortex-A9
GPU Mali-400
Sensors Accelerometer, proximity, compass
Messaging SMS(threaded view), MMS, Email, Push Email, IM
Browser HTML5, Adobe Flash
Radio Stereo FM radio with RDS
GPS Yes, with A-GPS support
Java Yes, via Java MIDP emulator
Colors White, Black, Yellow
- SNS integration
- MP4/H.263/H.264/WMV player
- MP3/eAAC+/WMA/WAV player
- TrackID music recognition
- Google Search, Maps, Gmail,
YouTube, Calendar, Google Talk
- Document viewer
- Voice memo/dial/commands
- Predictive text input

Xperia Acro Specifications

Xperia Acro

GENERAL 2G Network GSM 850 / 900 / 1800 / 1900
3G Network HSDPA 850 / 900 / 1900 / 2100
Announced 2012, May
Status Coming soon. Exp. release 2012, Q3
BODY Dimensions 126 x 66 x 11.9 mm
Weight 147 g
- Touch-sensitive controls
DISPLAY Type LED-backlit LCD, capacitive touchscreen, 16M colors
Size 720 x 1280 pixels, 4.3 inches (~342 ppi pixel density)
Multitouch Yes, up to 10 fingers
Protection Scratch-resistant shatter proof glass
- Sony Mobile BRAVIA Engine
- IP67 certified – dust and water proof (up to 1m for 30 mins)
SOUND Alert types Vibration; MP3 ringtones
Loudspeaker Yes
3.5mm jack Yes
MEMORY Card slot microSD, up to 32 GB
Internal 16 GB storage (11 GB user available), 1 GB RAM
DATA GPRS Up to 86 kbps
EDGE Up to 237 kbps
Speed HSDPA, 14.4 Mbps; HSUPA, 5.8 Mbps
WLAN Wi-Fi 802.11 b/g/n, Wi-Fi Direct, DLNA, Wi-Fi hotspot
Bluetooth Yes, v3.0 with A2DP, EDR
NFC Yes
USB Yes, microUSB v2.0, USB On-the-go support
CAMERA Primary 12 MP, 4000×3000 pixels, autofocus, LED flash
Features Geo-tagging, touch focus, face and smile detection, 3D sweep panorama, image stabilization
Video Yes, 1080p@30fps, continuous autofocus, video light, video stabilizer
Secondary Yes, 1.3 MP, 720p@30fps
FEATURES OS Android OS, v4.0 (Ice Cream Sandwich)
Chipset Qualcomm MSM8260 Snapdragon
CPU Dual-core 1.5 GHz Scorpion
GPU Adreno 220
Sensors Accelerometer, gyro, proximity, compass
Messaging SMS (threaded view), MMS, Email, IM, Push Email
Browser HTML5, Adobe Flash
Radio Stereo FM radio with RDS
GPS Yes, with A-GPS support and GLONASS
Java Yes, via Java MIDP emulator
Colors White, Black, Pink
- SNS integration
- HDMI port
- Active noise cancellation with dedicated mic
- MP4/H.263/H.264/WMV player
- MP3/eAAC+/WMA/WAV player
- TrackID music recognition
- Google Search, Maps, Gmail,
YouTube, Calendar, Google Talk
- Document viewer
- Voice memo/dial/commands
- Predictive text input

Check this link for the official ad.

http://www.hindustantimes.com/technology/PersonalTech-Updates/Sony-Xperia-go-Xperia-acro-S-waterproof-phones-hit-markets/SP-Article1-864267.aspx