25

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
adithi
  • 973
  • 3
  • 11
  • 19

4 Answers4

16

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);        
Peter
  • 5,556
  • 3
  • 23
  • 38
adithi
  • 973
  • 3
  • 11
  • 19
  • 3
    adithi waht is securemeAdmin and active ?i have same problem. can u explain – arpit Apr 25 '12 at 09:57
  • 3
    can anyone please say what is active and what is secureadmin here? – Reyjohn Jul 20 '12 at 20:37
  • This above code is taken from DeviceAdminSample present in Android sdk samples that are available with Android SDK. APIDemos-->app – Sundeep Jun 16 '13 at 10:52
  • Would have seriously helped save a lot of time had you just pasted what securemeAdmin was. – Johann Aug 24 '13 at 16:31
  • The complete code can be seen in the accepted answer of this thread: http://stackoverflow.com/questions/8987847/devicepolicymanager-locknow-is-not-working-for-motorola-tablets – takluiper Aug 04 '16 at 11:49
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();
VidaLux
  • 150
  • 3
1

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);
  }
 }
}
Ameer Moaaviah
  • 1,530
  • 12
  • 27
-4
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.screenBrightness = 0;
        getWindow().setAttributes(lp);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); 
Hawk
  • 551
  • 8
  • 10