title: How to lock phone on a button click event? - android
body: <p>I want my application to lock the phone when a button is clicked !
  Following is my code.</p>
  
  <pre><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;
          }
  
      }
  
  }
  </code></pre>
  
  <p>This is not working. I also tried with the following code in onClick event.</p>
  
  <pre><code>PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
           wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
          wl.release();
  </code></pre>
  
  <p>This is also not working. Can anyone help me ?</p>