How can I detect user pressing HOME key in my acti

2019-01-02 15:33发布

This question already has an answer here:

Can you please tell me how can my activity detect user pressing HOME key?

Thank you.

标签: android
11条回答
无色无味的生活
2楼-- · 2019-01-02 15:49

It can be done(uptil 2.3 atleast). Tested on android version 2.3.5 non-rooted HTC WildFire-S. Code snippet to catch/disable all the control keys :

@Override
public void onAttachedToWindow() {
    super.onAttachedToWindow();        
    this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);


}

public boolean onKeyDown(int keyCode, KeyEvent event) {     

    if(keyCode == KeyEvent.KEYCODE_HOME || keyCode == KeyEvent.KEYCODE_POWER || keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_MENU || keyCode == KeyEvent.KEYCODE_SEARCH  )
    {

       Toast.makeText(getApplicationContext(),"You pressed a control button with keyCode : " + keyCode, Toast.LENGTH_SHORT).show();
       return false;

    }
    else
    {
        Toast.makeText(getApplicationContext(),"You pressed" + keyCode, Toast.LENGTH_SHORT).show();
    }

    return true;
}

public boolean onKeyUp(int keyCode, KeyEvent event) {     

    if(keyCode == KeyEvent.KEYCODE_HOME || keyCode == KeyEvent.KEYCODE_POWER || keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_MENU || keyCode == KeyEvent.KEYCODE_SEARCH  )
    {

       return false;

    }
    else
    {
        // Do nothing
    }

    return true;
}
查看更多
几人难应
3楼-- · 2019-01-02 15:58

You can detect a HOME button press via Activity.onUserLeaveHint(). This method is called in two situations; when the user presses HOME and when a new activity is started. Make sure to somehow differenciate between the two.

查看更多
千与千寻千般痛.
4楼-- · 2019-01-02 16:02

use onUserLeaveHint() method in your Activity

@Override
protected void onUserLeaveHint()
{
   super.onUserLeaveHint();
}

http://developer.android.com/reference/android/app/Activity.html#onUserLeaveHint()

查看更多
裙下三千臣
5楼-- · 2019-01-02 16:04
android.text.method.KeyListener.onKeyDown([view],[text],KEYCODE_HOME,[event])

I'm not sure how the other parameters fit in, or even how to implement the above, but the info is all at the keylistner documentation.

But they also mention at another page that the home key ALWAYS goes home, you can't change that. So if you plan on having some kind of "are you sure you want to quit" dialog, that wouldn't work.

查看更多
其实,你不懂
6楼-- · 2019-01-02 16:06

You can write a Broadcast listener and set the filter to

<category android:name="android.intent.category.HOME" />

From the Broadcast receiver start ur activity/ notify the activity.

查看更多
深知你不懂我心
7楼-- · 2019-01-02 16:07
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;
    }



 public void onPause() {
    super.onPause();
    if(isApplicationSentToBackground(this))
    {
        Log.i("Home", "Home Pressed..!");
    }
 }
查看更多
登录 后发表回答