24

我一直在尝试通过程序锁定设备。但我仍然找不到解决方案。我想通过程序锁定Android froyo2.2。我已经尝试过 keyguardmanager 和 DeviceAdminManager。

我的应用是远程锁定设备。当收到带有一些要锁定的密码字的消息时,它会锁定手机。我找到了许多 Api_demo 程序作为解决方案,但我无法单独从中提取锁定代码并找到解决方案。

4

4 回答 4

16

活动类应该是内部类,外部类应该扩展 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);        
于 2011-03-26T06:42:50.380 回答
2

为了解决此任务,您可以查看NoKeyGuard 源代码,更准确地说,查看 NoKeyGuard Service 类和 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();
于 2012-05-10T13:42:45.343 回答
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);
  }
 }
}
于 2013-03-07T05:04:29.373 回答
-4
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.screenBrightness = 0;
        getWindow().setAttributes(lp);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); 
于 2013-04-16T07:21:21.863 回答