我DevicePolicyManager
在需要时用来锁定设备。我想要的是调出锁定屏幕,但使用DevicePolicyManager
和调用它的方法lockNow()
将锁定设备并关闭屏幕。如何防止关闭屏幕?
更新:我试过这个:
public class LockMeNowActivity extends Activity {
protected PowerManager.WakeLock mWakeLock;
private DevicePolicyManager mgr = null;
private ComponentName cn = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(com.commonsware.android.lockme.R.layout.main);
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
cn = new ComponentName(this, AdminReceiver.class);
mgr = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);
}
public void lockMeNow(View v) {
if (mgr.isAdminActive(cn)) {
mgr.lockNow();
} else {
Intent intent = new Intent(
DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, cn);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
"For experimentation purposes only");
startActivity(intent);
}
}
public void pullWakeLock() {
mWakeLock.acquire();
}
public void onPause() {
super.onPause();
pullWakeLock();
}
public void onResume() {
super.onResume();
releaseWakeLock();
}
public void releaseWakeLock() {
mWakeLock.release();
}
}