问题
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?
回答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
来源:https://stackoverflow.com/questions/4793339/lock-an-android-phone