The user expects my app to switch off the screen after being used. At the moment, I achieve this with Device Administrator rights and DevicePolicyManager.lockNow()
, which works fine if the security settings are set to PIN/Pattern/FaceUnlock, etc.
However, if using Slide/None, the above command just takes the user to the homescreen (or doesn't do anything), which is understandable, since there's nothing to "lock". Is there any way to achieve turning off the screen in such a situation? My app requires SDK>=16, if that matters.
So I guess my question is: How can an app reliably switch off the screen (I'm not holding a wakelock
, I'm using the WindowManager
-flags FLAG_TURN_SCREEN_ON
in onAttachedToWindow()
).
The "flow" of my app is:
- Activity is started by an intent while the screen is off, shows above the keyguard/switches on the screen with the above-mentioned flags
- User actively dismisses my Activity, I'm calling lockNow()
and finish()
and the user expects the screen to turn off. If the user is using the none/slide lock, this doesn't work and instead the user's homescreen is shown
Thanks!
To begin with see here:
To control this policy, the device admin must have a "force-lock" tag in the "uses-policies" section of its meta-data.
The calling device admin must have requested USES_POLICY_FORCE_LOCK to be able to call this method; if it has not, a security exception will be thrown.
Depending from the code you got, here's a pretty nice explanation of what can be wrong in your case (of course any code provided here will be of use!).
I've heard on several occasions, that calling twice the code DevicePolicyManager.lockNow()
will do the trick and here's one way of doing that:
mDPM = (DevicePolicyManager)getApplicationContext().getSystemService("device_policy");
Handler handlerUI = new Handler();
handlerUI.postDelayed(new Runnable() {
@Override
public void run() {
mDPM.lockNow();
}
}, 200);
finish();
mDPM.lockNow();
Here I found a more elaborate version of the same thing:
Android DevicePolicyManager lockNow() problem
public class SMSMessagingActivity extends Activity {
/** Called when the activity is first created. */
public static DevicePolicyManager mDPM;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
}
public static void LockNow(){
mDPM.lockNow();
}
}
ComponentName devAdminReceiver; // this would have been declared in your class body
// then in your onCreate
mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
devAdminReceiver = new ComponentName(context, deviceAdminReceiver.class);
//then in your onResume
boolean admin = mDPM.isAdminActive(devAdminReceiver);
if (admin)
mDPM.lockNow();
else Log.i(tag,"Not an admin");
Let's hope that the last workaround will work correctly.
Cheers
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With