I know, I know, this is a thing that I shouldn't be doing... but I do it anyway as my app is for toddler and I need to block access to home key.
As I found here on SO, to block the home button I simply need to do:
@Override
public void onAttachedToWindow()
{
super.onAttachedToWindow();
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
}
The main drawback of this is that the status bar is visible, even in in manifest file I've set it to be full screen
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
One solution would be to set full screen mode in code, so I do
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
this开发者_如何学Go.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
So far so good, the app keeps running in full screen. One major problem tho, if the user presses Power button the phone enters in standby. When press again and unlock the screen, the application has the status bar visible again. I can't set window parameters again as it already has content. Also, I am thinking to disable the Power button but I don't know how. I can intercept it being pressed onKeyDown
but how should I make it not do anything ?
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_POWER:
????????
default:
return false;
}
}
Any ideas on those matters ? Thank you all.
Actually, after doing a little bit of deep testing it seems that setting
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
produces unwanted behavior like: no Toast messages, unresponsive application, as said before, not allowing fullscreen app. So I just decided to keep Home button as it is by default, enabled. It would have been really nice from Google to offer possibility to disable the button... even if they would have need to make a special permission for it.
精彩评论