title: How to Lock/Unlock screen programmatically?
body: <p>I am doing an application which Locks the screen on shake. Now it is locking and from there it going to a broadcast receiver from there if the screen is off its entering into a service which has to turn the screen on.</p>
  
  <p>Below is the broadcast receiver:</p>
  
  <pre><code>  public class ScreenReceiver extends BroadcastReceiver {   
      public static boolean wasScreenOn = true;
      @Override
      public void onReceive(Context context, Intent intent) {
  
          System.out.println("Entered Broadcaste Reciever");
  
          if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
              // DO WHATEVER YOU NEED TO DO HERE
               System.out.println("SCREEN_OFF"+wasScreenOn);
              wasScreenOn = false;
  
              Intent i = new Intent(context, UpdateService.class);
              i.putExtra("screen_state", wasScreenOn);
              context.startService(i);
  
              System.out.println("jrkejhr keh");
          }
          else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
              // AND DO WHATEVER YOU NEED TO DO HERE
              wasScreenOn = true;
              System.out.println("SCREEN_ON"+wasScreenOn);
          }
      }
  </code></pre>
  
  <p>And its entering to a service where i had written the intent action to go home is...</p>
  
  <pre><code>  ShakeListener mShaker;
      int amountOfTime = 0;
      Context context1;   
      @Override   
           public void onCreate() {
  
              super.onCreate();
  
              // REGISTER RECEIVER THAT HANDLES SCREEN ON AND SCREEN OFF LOGIC
              System.out.println("Enterd Service");
              final Vibrator vibe = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
  
              mShaker = new ShakeListener(this);
              mShaker.setOnShakeListener(new ShakeListener.OnShakeListener () {
                public void onShake() {
                  vibe.vibrate(100);
                  Intent goHome = new Intent();
                  goHome.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                  goHome.setAction("android.intent.action.MAIN");
                  goHome.addCategory("android.intent.category.HOME");
                  startActivity(goHome);                      
                 }
              });
           }
         @Override
          public IBinder onBind(Intent intent) {
              // TODO Auto-generated method stub
              return null;
       }
  </code></pre>
  
  <p>It is entering into the service. But home screen is not displaying. When the service is invoked the the screen is locked.</p>