How to disable Home button without using the TYPE_

2019-01-31 15:35发布

i create a lockscreen application and i need to disable a home button, so if that phone is stolen, that phone can't be accessed.. my lockscreen is a fullscreen activity.. im use this code to disable a home button, but it gave me some bug. here's the code:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    }

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

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return false;
    }

that code gave me some bug like a notification/status area still can be accessed even my activity on the full screen mode, if i turn off my display and turn on it again.. the bug is like this :

first time application started: (still no problem)

enter image description here

after i turn off my screen from power button and turn it on again: enter image description here

the main problem is on the lockscreen.. when the notification area still can be accessed, then the lockscreen is not useful..

any idea how to solve this?? please help..

I am also facing the same problem when i press the end key button.

7条回答
在下西门庆
2楼-- · 2019-01-31 15:45

This is the working for the above issue..

@Override
public void onAttachedToWindow() {
    this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
    KeyguardManager keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
    KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
    lock.disableKeyguard();
    }

Add android.permission.DISABLE_KEYGUARD permission and give android:theme="@android:style/Theme.NoTitleBar.Fullscreen" to Application

查看更多
Emotional °昔
3楼-- · 2019-01-31 15:45

In my Samusung Pocket, nothing above worked fine. I could make it finally after further search.

I put full screen them in your AndroidMainfest.xml like following (not in Acitivity code):

<activity
    android:name=".geo.activity.LockActivity"
    android:theme = "@android:style/Theme.Black.NoTitleBar.Fullscreen" />

And use keygurad onAttachedToWindow() method in your activity:

@Override
public void onAttachedToWindow() {
    this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
    KeyguardManager keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
    KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
    lock.disableKeyguard();
}

Exactly what I have wanted. Blocking HOME button and same after turning off/on.

查看更多
The star\"
4楼-- · 2019-01-31 15:51

It's very simple, you should disable keyguard in the onAttachedToWindow() method:

KeyguardManager keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
lock.disableKeyguard();
查看更多
Evening l夕情丶
5楼-- · 2019-01-31 15:52

You can not control the behaviour of Home Button. It will do it's task and you need to adjust your app requirements.

For full screen add this in your activity tag in your manifest file:

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
查看更多
爱情/是我丢掉的垃圾
6楼-- · 2019-01-31 15:59

Try this code :

@Override
 public void onAttachedToWindow() {
  // TODO Auto-generated method stub
     super.onAttachedToWindow();  

     handler.postDelayed(mUpdateUiMsg, 100);

 }


 public boolean onKeyDown(int keyCode, KeyEvent event) {
  // TODO Auto-generated method stub
  if(keyCode==KeyEvent.KEYCODE_BACK){
   return true;
  }
  if(keyCode==KeyEvent.KEYCODE_HOME){
   return true;
  }

  return super.onKeyDown(keyCode, event);
 }


 private Runnable mUpdateUiMsg = new Runnable() {
        public void run() {


            getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);


         }
    };
查看更多
ら.Afraid
7楼-- · 2019-01-31 16:08

For a lockscreen Why don't you just use the following:

@Override
public void onAttachedToWindow() {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
}

If the user doesn't have a secure lock screen set, the app will just go to the home screen when your app closes. If the user does have a secure lockscreen set then the standard secure lockscreen will come up next no matter how your app closes. I guess I wouldn't worry about disabling the buttons. The user should be allowed to use the standard security features anyways, because they provide more security then you can guarantee from your app. Plus you don't have to spend the time coding secure unlock features.

查看更多
登录 后发表回答