5

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.

Mr Nice
  • 524
  • 8
  • 24
Michael Frans
  • 613
  • 3
  • 23
  • 49

7 Answers7

4

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

Mr Nice
  • 524
  • 8
  • 24
4

For my phone TYPE_KEYGUARD seems to override the fullscreen, no titlebar theme. The notification bar is always present. Try this:

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

Make your view stretch the entire screen and it will cover up the notification area. Your notification area may still be clickable (invisibly) but I believe if you catch all the key events on your view it should not propagate down to the bar.

Eric Chen
  • 3,562
  • 7
  • 39
  • 58
  • Hello @e_x_p i am also facing the same problem. your solution is also not working after end button pressed. After end button pressed the notification area is visible again. – Mr Nice Oct 16 '12 at 13:13
  • 1
    TYPE_KEYGUARD is deprecated in versions above SDK 14. It won't work at all in target ADK version 21. – Kristy Welsh Dec 15 '15 at 22:37
  • @KristyWelsh Exactly TYPE_KEYGUARD is deprecated in versions above SDK 14. Did you find any alternative solution for that? Thanks in advance. – sandeepmaaram Jan 21 '16 at 07:40
  • @SandeepMaram http://stackoverflow.com/questions/31081968/illegalargumentexception-window-type-can-not-be-changed-after-the-window-is-add/34911898#34911898 – Kristy Welsh Jan 21 '16 at 13:17
  • @SandeepMaram http://stackoverflow.com/questions/34300546/min-target-must-be-less-than-14-when-windowmanager-layoutparams-type-keyguard-us?lq=1 – Kristy Welsh Jan 21 '16 at 13:17
  • 1
    java.lang.IllegalArgumentException: Window type can not be changed after the window is added. – Pethő Jonatán May 22 '16 at 07:20
2

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.

sunghun
  • 1,424
  • 4
  • 25
  • 49
1

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();
Denys
  • 11
  • 1
1

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);


         }
    };
Prashant Kadam
  • 1,207
  • 4
  • 18
  • 30
1

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.

Kyle O.
  • 376
  • 2
  • 7
0

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"
Vineet Shukla
  • 23,865
  • 10
  • 55
  • 63
  • i can disable the home button with this: @Override public void onAttachedToWindow() { this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD); super.onAttachedToWindow(); } but it give me some bug like on the picture above.. any idea? – Michael Frans Sep 28 '11 at 12:58
  • thanks for the answer.. i have add that code already for make a fullscreen activity, but the problem is when i turn off my screen and turn it on again, it will gave me some bug like the picture above.. :( – Michael Frans Sep 28 '11 at 13:07
  • Hi Michael....Are you sure the above code u provided can disable the home button..coz i used the same code but the home button is not disabled....can u please help me? – user632905 Mar 24 '12 at 19:33
  • You certainly can control the Home button. Maybe do a search of SO before posting such claims. – Johann Aug 25 '13 at 07:52