但是,如果使用幻灯片/无,上述命令只是让用户进入主屏幕(或不做任何事情),这是可以理解的,因为没有什么可以“锁定”.在这种情况下有没有办法实现关闭屏幕?我的应用需要SDK> = 16,如果重要.
所以我想我的问题是:应用程序如何可靠地关闭屏幕(我没有持有一个wakelock,我在onAttachedToWindow()中使用WindowManager标志FLAG_TURN_SCREEN_ON).
我的应用程序的“流程”是:
– 在屏幕关闭的情况下,通过意图开始活动,在屏幕上方的键盘上/上面显示上述标志
– 用户主动关闭我的活动,我打电话给lockNow()和finish(),用户期望屏幕关闭.如果用户使用无/幻灯片锁定,则不起作用,而是显示用户的主屏幕
谢谢!
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.
根据你的代码,here’s是一个非常好的解释,在你的情况下可能是错误的(当然这里提供的任何代码将会有用).
我已经听到过几次,调用两次代码DevicePolicyManager.lockNow()将会做这个伎俩和here’s这样做的一种方式:
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我发现一个更精细的版本是同样的事情:
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");
我们希望最后一个解决方法能够正常运行.
干杯
相关文章
转载注明原文:android – DevicePolicyManager.lockNow();当安全设置设置为“幻灯片/无”时,不会关闭屏幕 - 代码日志