Android device lock programmatically

I tried to lock the device using the program. But I still can not find a solution. I want to block Android froyo2.2 through a program. I tried keyguardmanager and DeviceAdminManager.

My app is to remotely lock a device. When you receive a message with some code words to block, it locks the phone. I found many Api_demo programs as a solution, but I cannot extract the lock code from this one and find a solution.

+22
android android-emulator locking device-admin
Dec 28 '10 at 9:41
source share
4 answers

The activity class must be an inner class, and the outter class must 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 if you use to lock

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

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

 Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mDeviceAdminSample); 
+15
Mar 26
source share

To solve this problem, you can take a look at the source code of NoKeyGuard and more precisely at the NoKeyGuard service class and the KeyguardLockWrapper class.

To unlock the device, write the code if 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
May 10 '12 at 13:42
source share

The activity class must be an inner class, and the outer class must 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); } } } 
+1
Mar 07 '13 at 5:04 on
source share
  WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.screenBrightness = 0; getWindow().setAttributes(lp); getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); 
-3
Apr 16 '13 at 7:21
source share



All Articles