0

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.

adithi
  • 145
  • 5
  • 12
  • What error? tell them over here! – Aman Alam Dec 15 '10 at 06:36
  • Error(361): java.lang.SecurityException: No active admin owned by uid 10045 for policy #3 WARN/DeviceAdminAdd(128): Unable to retrieve device policy ComponentInfo{com.lock..} – adithi Dec 16 '10 at 05:01

3 Answers3

1

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.

Kishore
  • 952
  • 2
  • 11
  • 31
1

You can lock the Android's screen programmatically using the LockScreen class like so:

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

Take a look at the LockScreen class here.

Tyler Treat
  • 14,640
  • 15
  • 80
  • 115
  • already i tried the code which you mentioned.but there is no result and also no error.the log cat message is as follows 12-16 10:20:31.687: DEBUG/AndroidRuntime(332): Shutting down VM 12-16 10:20:31.697: DEBUG/dalvikvm(332): Debugger has detached; object registry had 1 entries 12-16 10:20:31.817: INFO/AndroidRuntime(332): NOTE: attach of thread 'Binder Thread #3' failed 12-16 10:20:37.376: DEBUG/dalvikvm(125): GC_EXPLICIT freed 2660 objects / 175544 bytes in 101ms 12-16 10:20:53.036: DEBUG/SntpClient(59): request time failed: java.net.SocketException: Address family not supported by protocol – adithi Dec 16 '10 at 04:51
  • 1
    i have used same code of yours and i got the security warning so i have given this permission in menifest file. . but now its not giving me any exception and not even showing the lock. do you know what is the problem? – sajjoo Mar 02 '11 at 13:06
  • @sajjoo i got same problem, did you find any solution? – Bhupinder Aug 29 '13 at 11:55
0

@Bhupinder Kindly check the following link.

http://musicm122.blogspot.in/2011/10/locking-and-unlocking-android-phone.html

//Get the window from the context

   WindowManager wm = Context.getSystemService(Context.WINDOW_SERVICE);

   //Unlock

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

   Window window = getWindow();  

   window.addFlags(wm.LayoutParams.FLAG_DISMISS_KEYGUARD);  



   //Lock device

   DevicePolicyManager mDPM;

   mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
sajjoo
  • 6,576
  • 20
  • 65
  • 86
  • Note that [link-only answers](http://meta.stackoverflow.com/tags/link-only-answers/info) are discouraged, SO answers should be the end-point of a search for a solution (vs. yet another stopover of references, which tend to get stale over time). Please consider adding a stand-alone synopsis here, keeping the link as a reference. – kleopatra Aug 30 '13 at 07:31