Tuesday, March 5, 2013

Disable home button in android.

In android, There is no direct way to disable  the home button.

As all android developer says it can't be done .

Developer site says : "public static final int KEYCODE_HOME
Key code constant: Home key. This key is handled by the framework and is never delivered to applications."

This code only work before android 4.0.

 @Override
    public void onAttachedToWindow() {
     
        this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
        super.onAttachedToWindow();
//        this.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
    }
   
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {    
        if(keyCode == KeyEvent.KEYCODE_HOME)
        {

            Toast.makeText(this, "Now we can handle it.", Toast.LENGTH_LONG).show();
           //The Code Want to Perform.

        }
        return false;
    }



Post ICS i.e. Android 4+, the overriding of the HomeButton has been removed for security reasons, to enable the user exit in case the application turns out to be a malware.

There is used to disable home button in android but
this security flaw has been fixed in newer versions of Android (4.0+) so it will not work in ICS and jelly bean...!!

 Workaround that we can do to disable home screen :


1 - you can declare the activity as a Launcher , so that when the HomeButton is pressed it will simply restart your application and remain there itself (the users would notice nothing but a slight flicker in the screen).

 2 -
Write a second app that implements the home screen, when the home screen button is pressed this app will come to the foreground. From this app you then need to push your main app back to the foreground. The only catch is that your Home screen app must never need an update, but you should be able to update the main app freely without the tab asking to set the home launcher.
Hope that makes sense..

3-
There are few things that you can try:
  • You can set your activity single top, and start it over with clear to top flag when onPause() method is called, this will block the home button and opening other activities.
  • Listen to BOOT_COMPLETED broadcast to start your activity - this will protect you from users who will take the battery out of the device in order to reboot it.
  • Add Alarmmanager that will test every second if your app is alive and if it is not, then start it - This will protect you from userers that some how managed to close your app(may be with external tools).
Do this and no one be able to exit your app.

4-

To tackle the HOME key. For your application set the manifest as
        <action android:name="android.intent.action.MAIN" />              
            <category android:name="android.intent.category.HOME" />                 
            <category android:name="android.intent.category.DEFAULT" />               
            <category android:name="android.intent.category.MONKEY"/>
Now your application is an alternate Launcher application.
Use the adb, and disable the launcher application using package manager
pm disable com.android.launcher2
Now the Home key press will always stay in the same screen.