0

I have to lock emulator when I click the button. I use this code-

I don't understood what are active and securemeAdmin

I use this link-> Lock the android device programmatically

public class adminActivity extends DeviceAdminReceiver
{
   public static class Test extends Activity
   {
      Button btn;
      DevicePolicyManager mDPM;
      ComponentName mDeviceAdminSample;

      @Override
      protected void onCreate(Bundle savedInstanceState) 
      {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);

     mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
         mDeviceAdminSample = new ComponentName(Test.this,
                adminActivity.class);

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

         btn=(Button)findViewById(R.id.btn);

         btn.setOnClickListener(new OnClickListener() 
         {
        @Override
        public void onClick(View v) 
        {
           if (active) 
           {
            mDPM.lockNow();
           }
        }
         });
      }
   }
}

I also use keyguard manager but it also not working. Can someone give me example for lock phone?

public class Keyguard extends Activity 
{
    Button btn;
    KeyguardLock lock;
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btn=(Button)findViewById(R.id.btn);

        KeyguardManager mgr = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE); 
        lock = mgr.newKeyguardLock(KEYGUARD_SERVICE);

        btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) 
        {
            lock.reenableKeyguard();
        }
    });
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
arpit
  • 555
  • 1
  • 7
  • 25
  • possible duplicate of [Lock the android device programatically](http://stackoverflow.com/questions/4545079/lock-the-android-device-programatically) – Nanne Apr 25 '12 at 10:06
  • yes @nanna but still searching for solution. – arpit Apr 25 '12 at 10:12

2 Answers2

3

This code will work for you..

WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = 0;
getWindow().setAttributes(lp);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); 
Nate
  • 31,017
  • 13
  • 83
  • 207
Khushi
  • 147
  • 1
  • 8
1

For locking android device you must need admin right. You have to take care of three things 1) DeviceAdminReceiver class 2)Manifest file 3)Activity class where you call lock method of DevicePolicyManager. Refer this link http://developer.android.com/guide/topics/admin/device-admin.html

RKS
  • 445
  • 4
  • 14