title: Unlock device, display a text, then lock again
body: <p>For the need of my application, I need to display a message on the screen even if the lockscreen is enabled, then wait 3 seconds, than I have to lock again the phone as I don't want it to make unwanted phone calls in your pockets.</p>
  
  <p>First part is easy:</p>
  
  <pre><code>if (PreferenceManager.getDefaultSharedPreferences(
      getBaseContext()).getBoolean("wake", false)) {
      KeyguardManager kgm = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
      boolean isKeyguardUp = kgm.inKeyguardRestrictedInputMode();
      WakeLocker.acquire(ProtoBenService.this);
      Intent myIntent = new Intent(ProtoBenService.this,LockActivity.class);
      myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  
      if (isKeyguardUp) {
          ProtoBenService.this.startActivity(myIntent);
      } else
      Toast.makeText(ProtoBenService.this.getBaseContext(), intention, Toast.LENGTH_LONG).show();
  
      WakeLocker.release();
  }
  </code></pre>
  
  <p>With this class:</p>
  
  <pre><code>public abstract class WakeLocker {
      private static PowerManager.WakeLock wakeLock;
  
      public static void acquire(Context ctx) {
          if (wakeLock != null) wakeLock.release();
  
          PowerManager pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE);
          wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |
                  PowerManager.ACQUIRE_CAUSES_WAKEUP |
                  PowerManager.ON_AFTER_RELEASE, "CobeIm");
          wakeLock.acquire();
      }
  
      public static void release() {
          if (wakeLock != null) wakeLock.release(); wakeLock = null;
      }
  }
  </code></pre>
  
  <p>And the Activity:</p>
  
  <pre><code>public class LockActivity extends Activity {
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          Window window = getWindow();
          window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
          window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
          window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
          TextView tv = new TextView(this);
          tv.setText("This is working!");
          tv.setTextSize(45);
          setContentView(tv);
  
          Runnable mRunnable;
          Handler mHandler = new Handler();
          mRunnable = new Runnable() {
              @Override
              public void run() {
                  LockActivity.this.finish();
              }
          };
          mHandler.postDelayed(mRunnable, 3 * 1000);
      }
  }
  </code></pre>
  
  <p>So, this is nice, the phone can display my text!</p>
  
  <p>The only problem comes when I want to lock again the phone, it seems that locking the phone is protected by the system...</p>
  
  <ul>
  <li><a href="https://groups.google.com/forum/?fromgroups=#!topic/android-developers/X7BGaSG_aww" rel="nofollow noreferrer">Programmatically turning off the screen and locking the phone</a>  </li>
  <li><a href="https://stackoverflow.com/questions/4447312/how-to-lock-the-android-programatically">how to lock the android programatically</a></li>
  </ul>
  
  <p>I think that my users won't understand the Device Admin and won't be able to activate it. Is there any workaround to lock the screen without the Device Admin stuff?</p>