@Override
  public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();

    mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    try {
      mStartForeground = getClass().getMethod("startForeground", mStartForegroundSignature);
      mStopForeground = getClass().getMethod("stopForeground", mStopForegroundSignature);
    } catch (NoSuchMethodException e) {
      // Running on an older platform.
      mStartForeground = mStopForeground = null;
    }

    startForegroundCompat(NOTIFICATION_ID, getNotification());
    try {

      analyzer = new SimpleAudioAnalyser();
      // start listening for data points added, and add sound to them
      dataManager = SleepDataManager.getInstance();
      dataManager.addObserver(analyzer, SleepDataManager.DATA_POINT_ADDED);
      timer = new Timer();
      timer.scheduleAtFixedRate(new DelayTask(), 500, 1000);
      analyzer.measureStart();
    } catch (Exception e) {
      stopSelf();
    }
    running = true;
    if (D) Log.e(TAG, "Created!");
  }
 @Override
 public void onDestroy() {
   // TODO Auto-generated method stub
   super.onDestroy();
   if (D) Log.e(TAG, "Destroyed");
   if (timer != null) timer.cancel();
   if (analyzer != null) {
     dataManager.unregisterObserver(analyzer, SleepDataManager.DATA_POINT_ADDED);
     analyzer.measureStop();
   }
 }