2

Possible Duplicate:
Lock the android device programatically

I want to be able to lock the Android phone with a password when I run a method. Does anyone have a reference or sample code for me to refer. Thanks

EDIT I have tried using

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

as said by the answer below, but I'm still trying to get it to add a password that I have specifically entered into the database at my server side, so the only way to unlock his phone is to enter the password that i set

EDIT

http://developer.android.com/reference/android/app/admin/DevicePolicyManager.html

been trying to work on it ^

EDIT

I have seen that

device_admin_sample.xml

with the contents

<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
  <uses-policies>
    <limit-password />
    <watch-login />
    <reset-password />
    <force-lock />
    <wipe-data />
  </uses-policies>
</device-admin>

But where do I put this xml file at.. it seems to have an error wherever i put

EDIT

Now, I have implemented it halfway and put this on hold upon seeing the comment below that I cannot lock the phone with a password. But seeing the API documentation, there's a function to reset password with a new password.

resetPassword(String password, int flags)

So what is it? Can I implement password lock on the phone? or is the idea of locking the phone until a new password given by the server is entered, unable to be done?

Community
  • 1
  • 1
robobooga
  • 513
  • 6
  • 19
  • Similar question: http://stackoverflow.com/questions/733721/android-activation-of-the-system-key-lock also one same question is closed before: http://stackoverflow.com/questions/4552026/android-phone-lock-program So its better if you provide more information like what you have done until now and where you get stuck??? – Harry Joy Jan 25 '11 at 12:20
  • the link you added is broken. its http://developer.android.com/reference/android/app/admin/DevicePolicyManager.html – Harry Joy Jan 25 '11 at 13:16
  • It will be at "/platforms/android-/samples/..." as stated here: http://developer.android.com/resources/samples/ApiDemos/res/xml/device_admin_sample.html – Harry Joy Jan 25 '11 at 13:27

1 Answers1

1

Your app cannot stop anyone from pressing the home button and getting out of your app. This prevents malware or bad coded app to lock the phone to the point you need to remove the battery to get out of the crapware.

You can programmatically lock the screen with the usual screen locker though :

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

This will require the "Disable Keyguard" permission in your manifest file.

EDIT after OP refinement :

Take a look at this sample that shows you how to use the device admin manager : http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/DeviceAdminSample.html

Yahel
  • 8,522
  • 2
  • 24
  • 32
  • Does this allow user to key in a password that i set for them to unlock the phone? – robobooga Jan 25 '11 at 12:31
  • No, this just locks the screen as if they had put their phone to sleep. – Yahel Jan 25 '11 at 13:27
  • Sorry misread your question in the comment : If their phone is set to ask them a password or a touch gesture, then yes it will ask for it when trying to unlock the phone. – Yahel Jan 25 '11 at 13:31
  • @robobooga : did you even read the first part of his answer ? You can't put your own password, or it would be an open door for malwares to block the phone. – Valentin Rocher Jan 25 '11 at 14:04
  • so you mean that i cannot implement device admin manager? – robobooga Jan 25 '11 at 14:17
  • Dont use this one... Its deprecated now. Instead use DeviceManager API now – Noman Mar 19 '14 at 12:13