Lock the Android device programmatically

佐手、 提交于 2019-11-26 03:18:59

问题


I have been trying to lock the device through program. But I can\'t find the solution still. I want to lock Android froyo2.2 through program. I have tried keyguardmanager and DeviceAdminManager.

My app is to remote lock the device. When message is received with some code words to lock then it locks the phone. I have found many Api_demo program as solution but I can\'t extract lock code alone from that and find solution.


回答1:


The activity class should be inner class and the outter class should extend 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);
      }
   }
}

To lock the device write the code in the event where you use to lock

if (active) {
mDPM.lockNow();
}

If DeviceAdmin is enabled then the phone will be locked. To enable the device admin, the DevicePolicyManager intent is called and it should be enabled by the user.

Intent intent = new   Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);  
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mDeviceAdminSample);        



回答2:


In order to solve this task you can take a look to NoKeyGuard source code and more precisely to a NoKeyGuard Service class and KeyguardLockWrapper class.

To unlock the device write the code in the event where you use to unlock:

    Context context= getApplicationContext();
    KeyguardManager _guard = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
    KeyguardLock _keyguardLock = _guard.newKeyguardLock("KeyguardLockWrapper");
    //to disable
    _keyguardLock.disableKeyguard();
    //to enable
    _keyguardLock.reenableKeyguard();



回答3:


The activity class should be the inner class and the outer class should extend 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);
  }
 }
}



回答4:


        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.screenBrightness = 0;
        getWindow().setAttributes(lp);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); 


来源:https://stackoverflow.com/questions/4545079/lock-the-android-device-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!