how to lock the android programatically

后端 未结 3 656
挽巷
挽巷 2021-01-17 02:12

i want to lock the phone when i click the lock button.anybody please help with simple code.i tried with part of code from API_Demos but it shows some error.

3条回答
  •  隐瞒了意图╮
    2021-01-17 02:48

    The code:

    KeyguardManager mgr = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE); 
    KeyguardLock lock = mgr.newKeyguardLock(KEYGUARD_SERVICE); 
    lock.reenableKeyguard();
    

    will NOT lock the screen. It just enables the keyguard lock. When you run with

    lock.disableKeyguard();
    

    and press lock button on the device it will not lock the keyguard. To lock the screen programatically you have to refer to Device Admin and use locknow() method to lock the device.

提交回复
热议问题