我一直试图通过程序锁定设备。但我还是找不到解决方案。 我想通过程序锁定Android froyo2.2。我尝试过keyguardmanager和DeviceAdminManager。
我的应用是远程锁定设备。收到带有某些代码字的消息后,它会锁定电话。我发现很多Api_demo程序作为解决方案,但我不能单独提取锁定代码并找到解决方案。
答案 0 :(得分:15)
活动类应该是内部类,而outter类应该扩展DeviceAdminReceiver
public class adminActivity extends DeviceAdminReceiver {
public static class Controller extends Activity {
DevicePolicyManager mDPM;
ComponentName mDeviceAdminSample;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
mDeviceAdminSample = new ComponentName(Controller.this,
adminActivity.class);
}
}
}
要锁定设备,请在用于锁定的事件中编写代码
if (active) {
mDPM.lockNow();
}
如果启用了DeviceAdmin,则手机将被锁定。 要启用设备管理员,将调用DevicePolicyManager意图,并且应由用户启用它。
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mDeviceAdminSample);
答案 1 :(得分:3)
为了解决这个问题,您可以查看NoKeyGuard source code,更准确地说是NoKeyGuard服务类和KeyguardLockWrapper类。
要解锁设备,请在用于解锁的事件中编写代码:
Context context= getApplicationContext();
KeyguardManager _guard = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
KeyguardLock _keyguardLock = _guard.newKeyguardLock("KeyguardLockWrapper");
//to disable
_keyguardLock.disableKeyguard();
//to enable
_keyguardLock.reenableKeyguard();
答案 2 :(得分:1)
活动类应该是内部类,外部类应该扩展DeviceAdminReceiver
public class adminActivity extends DeviceAdminReceiver {
public static class Controller extends Activity {
DevicePolicyManager mDPM;
ComponentName mDeviceAdminSample;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
mDeviceAdminSample = new ComponentName(Controller.this,
adminActivity.class);
}
}
}
答案 3 :(得分:-3)
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = 0;
getWindow().setAttributes(lp);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);