title: android: broadcast receiver for screen on and screen off
body: <p>I was just wondering if it is possible to register a broadcast receiver that detects Screen ON/OFF in the application manifest.
  The reason why I don't like the programmable method is that it requires the app to be running in order to detect such a thing, while:
  "Applications with Broadcast Receivers registered in the manifest don’t have to be running when the Intent is broadcast for the receivers to execute" (source: Professional Android 2 Application Development book)</p>
  
  <p>My app is actually a lockscreen app which by using the programmable way needs to be running all the time :S</p>
  
  <p>Is there a way around it?</p>
  
  <p>I'm trying the following in the manifest:</p>
  
  <pre><code>&lt;receiver android:name=".MyBroadCastReciever"&gt;
      &lt;intent-filter&gt;
          &lt;action android:name="android.intent.action.SCREEN_OFF"/&gt;
          &lt;action android:name="android.intent.action.SCREEN_ON"/&gt;
      &lt;/intent-filter&gt;
  &lt;/receiver&gt;
  </code></pre>
  
  <p>and simple MyBroadCastReciever class:</p>
  
  <pre><code>public class MyBroadCastReciever extends BroadcastReceiver {
  
      @Override
      public void onReceive(Context context, Intent intent) {
          if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
              Log.i("Check","Screen went OFF");
              Toast.makeText(context, "screen OFF",Toast.LENGTH_LONG).show();
          } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
              Log.i("Check","Screen went ON");
              Toast.makeText(context, "screen ON",Toast.LENGTH_LONG).show();
          }
      }
  }
  </code></pre>