4

Through that code i am success to handle HomeKey in below 4.0 version but i have problem in handling Homekey in upper 4.0 version. so please help me to solve that issue to handle HomeKey press in upper 4.0 version.because it is not disable Home press.

@Override
    public void onAttachedToWindow() 
    {
        // TODO Auto-generated method stub
      if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) 
      {
        super.onAttachedToWindow();
        this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
             getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
}

    }   
Cԃաԃ
  • 1,259
  • 1
  • 15
  • 29
ravi152
  • 209
  • 1
  • 4
  • 13

2 Answers2

2

As @j__m said, TYPE_KEYGUARD is no longer supported. There are many other ways which have been discussed on other questions but don't work in latest API level. I'll save you the effort and would like to share some search, trial and error that I did. I tried many method but none of them worked for me in API level 17. I tried answers on

Call method when home button pressed on android,

Detect home button press in android and

Some of the ones I tried( including above answers) and didn't work were:

  1. Using keyCode==KeyEvent.KEYCODE_HOME in many ways as stated above. Now, if you read the documentation of KeyEvent.KEYCODE_HOME, it says that This key is handled by the framework and is never delivered to applications. So its no more valid now.

  2. I tried using onUserLeaveHint(). The documentation says: Called as part of the activity lifecycle when an activity is about to go into the background as the result of user choice.For example, when the user presses the Home key, onUserLeaveHint() will be called,but when an incoming phone call causes the in-call Activity to be automatically brought to the foreground.

    If you are not calling any activity from within your current activity, where you are detecting the home button then you might be able to use this approach. The issue with that is that the method also gets called when you start an Activity from within the activity where you are calling onUserleaveLint(), as was my case. See Android onBackPressed/onUserLeaveHint question for more. So its not sure that it would be call ONLY by pressing home button.

Finally the following worked for me :

Seeing How to check current running applications in Android?, you can say that if yours is the recent task that is shown on long pressing the home button, then it was send to background.( i.e home button was pressed).

So, In your onPause() of the activity where you are trying to detect the home button pressed, you can check whether the application has been sent to background.

@Override
public void onPause() {
    if (isApplicationSentToBackground(this)){
            // Home button pressed
        // Do what you want to do on detecting Home Key being Pressed 
    }
    super.onPause();
}

Function to check whether yours is the app which has been most recently sent to the background:

public boolean isApplicationSentToBackground(final Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningTaskInfo> tasks = am.getRunningTasks(1);
    if (!tasks.isEmpty()) {
        ComponentName topActivity = tasks.get(0).topActivity;
        if (!topActivity.getPackageName().equals(context.getPackageName())) {
            return true;
        }
    }
    return false;
}

Using this I was successfully able to detect the Home Button click. Hope this works for you as well.

Community
  • 1
  • 1
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
0

TYPE_KEYGUARD is no longer supported for non-system apps. The only way to handle the home key is to be the launcher.

j__m
  • 9,392
  • 1
  • 32
  • 56