'How to lock phone on a button click event? - android

I want my application to lock the phone when a button is clicked ! Following is my code.

public class MainActivity extends Activity implements OnClickListener {

    Button b;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b = (Button) findViewById(R.id.button);

        b.setOnClickListener(this);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        switch (arg0.getId()) {
        case R.id.button:
        KeyguardManager km = (KeyguardManager) getSystemService(Activity.KEYGUARD_SERVICE);
            KeyguardLock kl = km.newKeyguardLock(KEYGUARD_SERVICE);
            kl.reenableKeyguard();


            break;
        }

    }

}

This is not working. I also tried with the following code in onClick event.

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
         wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
        wl.release();

This is also not working. Can anyone help me ?



Solution 1:[1]

You need to set up a device admin component, then call lockNow() on the DevicePolicyManager.

This sample project shows what is required from a coding standpoint. Once installed, the user must agree to make your app be a device administrator. The activity will route the user to the proper screen in the Settings app for this if the app is not a device administrator:

public class LockMeNowActivity extends Activity {
  private DevicePolicyManager mgr=null;
  private ComponentName cn=null;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    cn=new ComponentName(this, AdminReceiver.class);
    mgr=(DevicePolicyManager)getSystemService(DEVICE_POLICY_SERVICE);
  }

  public void lockMeNow(View v) {
    if (mgr.isAdminActive(cn)) {
      mgr.lockNow();
    }
    else {
      Intent intent=
          new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
      intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, cn);
      intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
                      getString(R.string.device_admin_explanation));
      startActivity(intent);
    }
  }
}

Solution 2:[2]

Use this code in your onCreate() method of activity to initialize DevicePolicyManager :

 myDevicePolicyManager = (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 set a boolean enable :

if (enable) {
myDevicePolicyManager.lockNow();
}

May be you have to enable the device admin, the DevicePolicyManager intent is called and it should be enabled by the user. Follow this code :

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 CommonsWare
Solution 2 The Holy Coder