@Override
  public void onDestroy() {
    super.onDestroy();

    sensorHandler.removeCallbacksAndMessages(null);
    mSensorManager.unregisterListener(this, mAccelerometer);
    sensorThread.quit();

    wakeLock.release();

    unregisterReceiver(dataLabeler);

    if (Aware.DEBUG) Log.d(TAG, "Accelerometer service terminated...");
  }
  @Override
  public void onCreate() {
    super.onCreate();

    mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    powerManager = (PowerManager) getSystemService(POWER_SERVICE);

    mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

    TAG =
        Aware.getSetting(getApplicationContext(), Aware_Preferences.DEBUG_TAG).length() > 0
            ? Aware.getSetting(getApplicationContext(), Aware_Preferences.DEBUG_TAG)
            : TAG;

    if (Aware.getSetting(this, Aware_Preferences.FREQUENCY_ACCELEROMETER).length() > 0) {
      SAMPLING_RATE =
          Integer.parseInt(
              Aware.getSetting(getApplicationContext(), Aware_Preferences.FREQUENCY_ACCELEROMETER));
    } else {
      Aware.setSetting(this, Aware_Preferences.FREQUENCY_ACCELEROMETER, SAMPLING_RATE);
    }

    DATABASE_TABLES = Accelerometer_Provider.DATABASE_TABLES;
    TABLES_FIELDS = Accelerometer_Provider.TABLES_FIELDS;
    CONTEXT_URIS = new Uri[] {Accelerometer_Sensor.CONTENT_URI, Accelerometer_Data.CONTENT_URI};

    sensorThread = new HandlerThread(TAG);
    sensorThread.start();

    wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
    wakeLock.acquire();

    sensorHandler = new Handler(sensorThread.getLooper());
    mSensorManager.registerListener(this, mAccelerometer, SAMPLING_RATE, sensorHandler);

    IntentFilter filter = new IntentFilter();
    filter.addAction(ACTION_AWARE_ACCELEROMETER_LABEL);
    registerReceiver(dataLabeler, filter);

    if (mAccelerometer == null) {
      if (Aware.DEBUG) Log.w(TAG, "This device does not have an accelerometer!");
      Aware.setSetting(this, Aware_Preferences.STATUS_ACCELEROMETER, false);
      stopSelf();
      return;
    } else {
      saveAccelerometerDevice(mAccelerometer);
    }

    if (Aware.DEBUG) Log.d(TAG, "Accelerometer service created!");
  }