Q:

Android: Help counting when the screen is turning off/on in a background service

For a little app that I'm working at I need to count how many times the screen turns off and on and when it does that. I created a BroadcastReceiver that runs a Service after booting that is supposed to count that. 
This is my Service class:
public class MyService extends Service {

  private final IBinder mBinder = new MyBinder();
  public static boolean wasScreenOn = true;

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    //the next line is line 29
      if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
        Log.i("Screen off", String.valueOf(System.currentTimeMillis()));
            wasScreenOn = false;
        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            Log.i("Screen on", String.valueOf(System.currentTimeMillis()));
            wasScreenOn = true;
        }
      return Service.START_NOT_STICKY;
  }

  @Override
  public IBinder onBind(Intent arg0) {
  //TODO for communication return IBinder implementation
        return mBinder;
  }

  public class MyBinder extends Binder {
        MyService getService() {
          return MyService.this;
        }
      }

} 
The problem is that I get this error:
06-26 23:16:38.485: E/AndroidRuntime(1402): FATAL EXCEPTION: main
06-26 23:16:38.485: E/AndroidRuntime(1402): java.lang.RuntimeException: Unable to start service com.MyApp.MyService@40ce0078 with Intent { cmp=com.MyApp/.MyService (has extras) }: java.lang.NullPointerException
06-26 23:16:38.485: E/AndroidRuntime(1402):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2673)
06-26 23:16:38.485: E/AndroidRuntime(1402):     at android.app.ActivityThread.access$1900(ActivityThread.java:141)
06-26 23:16:38.485: E/AndroidRuntime(1402):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1331)
06-26 23:16:38.485: E/AndroidRuntime(1402):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-26 23:16:38.485: E/AndroidRuntime(1402):     at android.os.Looper.loop(Looper.java:137)
06-26 23:16:38.485: E/AndroidRuntime(1402):     at android.app.ActivityThread.main(ActivityThread.java:5041)
06-26 23:16:38.485: E/AndroidRuntime(1402):     at java.lang.reflect.Method.invokeNative(Native Method)
06-26 23:16:38.485: E/AndroidRuntime(1402):     at java.lang.reflect.Method.invoke(Method.java:511)
06-26 23:16:38.485: E/AndroidRuntime(1402):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-26 23:16:38.485: E/AndroidRuntime(1402):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-26 23:16:38.485: E/AndroidRuntime(1402):     at dalvik.system.NativeStart.main(Native Method)
06-26 23:16:38.485: E/AndroidRuntime(1402): Caused by: java.lang.NullPointerException
06-26 23:16:38.485: E/AndroidRuntime(1402):     at com.MyApp.MyService.onStartCommand(MyService.java:29)
06-26 23:16:38.485: E/AndroidRuntime(1402):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2656)

I imagine it is because when the Service starts there is still no Intent for Screen On/Off. But I want this Service to run in the background and wait for it. 
How should I do this?
Thanks a lot!

A:

I made it work after I used the code from this app 
Code:
public class HardwareTriggerService extends Service {
private HardwareTriggerReceiver hardwareTriggerReceiver;
@Override
public void onCreate() {
    super.onCreate();
    Log.e(">>>>>>", "HardwareTriggerService CREATED.");
    IntentFilter filter = new IntentFilter();
    filter.addAction(Intent.ACTION_SCREEN_ON);
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    hardwareTriggerReceiver = new HardwareTriggerReceiver();
    registerReceiver(hardwareTriggerReceiver, filter);
}

@Override
public void onDestroy() {
    super.onDestroy();
    Log.e(">>>>>>", "HardwareTriggerService DESTROYED.");
    IntentFilter filter = new IntentFilter();
    filter.addAction(Intent.ACTION_SCREEN_ON);
    filter.addAction(Intent.ACTION_SCREEN_OFF);

    unregisterReceiver(hardwareTriggerReceiver);
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}
}

