Set device lock
You can set the maximum period of user inactivity that can occur before the device locks. For example:
DevicePolicyManager mDPM;ComponentName mDeviceAdminSample;...long timeMs =1000L*Long.parseLong(mTimeout.getText().toString());
mDPM.setMaximumTimeToLock(mDeviceAdminSample, timeMs);
You can also programmatically tell the device to lock immediately:
DevicePolicyManager mDPM;
mDPM.lockNow();