private void saveAccelerometerDevice(Sensor acc) {
    Cursor accelInfo =
        getContentResolver().query(Accelerometer_Sensor.CONTENT_URI, null, null, null, null);
    if (accelInfo == null || !accelInfo.moveToFirst()) {
      ContentValues rowData = new ContentValues();
      rowData.put(
          Accelerometer_Sensor.DEVICE_ID,
          Aware.getSetting(getApplicationContext(), Aware_Preferences.DEVICE_ID));
      rowData.put(Accelerometer_Sensor.TIMESTAMP, System.currentTimeMillis());
      rowData.put(Accelerometer_Sensor.MAXIMUM_RANGE, acc.getMaximumRange());
      rowData.put(Accelerometer_Sensor.MINIMUM_DELAY, acc.getMinDelay());
      rowData.put(Accelerometer_Sensor.NAME, acc.getName());
      rowData.put(Accelerometer_Sensor.POWER_MA, acc.getPower());
      rowData.put(Accelerometer_Sensor.RESOLUTION, acc.getResolution());
      rowData.put(Accelerometer_Sensor.TYPE, acc.getType());
      rowData.put(Accelerometer_Sensor.VENDOR, acc.getVendor());
      rowData.put(Accelerometer_Sensor.VERSION, acc.getVersion());

      try {
        getContentResolver().insert(Accelerometer_Sensor.CONTENT_URI, rowData);

        Intent accel_dev = new Intent(ACTION_AWARE_ACCELEROMETER);
        accel_dev.putExtra(EXTRA_SENSOR, rowData);
        sendBroadcast(accel_dev);

        if (Aware.DEBUG) Log.d(TAG, "Accelerometer device:" + rowData.toString());
      } catch (SQLiteException e) {
        if (Aware.DEBUG) Log.d(TAG, e.getMessage());
      } catch (SQLException e) {
        if (Aware.DEBUG) Log.d(TAG, e.getMessage());
      }
    } else accelInfo.close();
  }
  @Override
  public void onSensorChanged(SensorEvent event) {
    ContentValues rowData = new ContentValues();
    rowData.put(
        Accelerometer_Data.DEVICE_ID,
        Aware.getSetting(getApplicationContext(), Aware_Preferences.DEVICE_ID));
    rowData.put(Accelerometer_Data.TIMESTAMP, System.currentTimeMillis());
    rowData.put(Accelerometer_Data.VALUES_0, event.values[0]);
    rowData.put(Accelerometer_Data.VALUES_1, event.values[1]);
    rowData.put(Accelerometer_Data.VALUES_2, event.values[2]);
    rowData.put(Accelerometer_Data.ACCURACY, event.accuracy);
    rowData.put(Accelerometer_Data.LABEL, LABEL);

    if (data_values.size() < 250) {
      data_values.add(rowData);

      Intent accelData = new Intent(ACTION_AWARE_ACCELEROMETER);
      accelData.putExtra(EXTRA_DATA, rowData);
      sendBroadcast(accelData);

      if (Aware.DEBUG) Log.d(TAG, "Accelerometer: " + rowData.toString());

      return;
    }

    data_buffer = new ContentValues[data_values.size()];
    data_values.toArray(data_buffer);

    try {
      if (Aware.getSetting(getApplicationContext(), Aware_Preferences.DEBUG_DB_SLOW)
          .equals("false")) {
        new AsyncStore().execute(data_buffer);
      }
    } catch (SQLiteException e) {
      if (Aware.DEBUG) Log.d(TAG, e.getMessage());
    } catch (SQLException e) {
      if (Aware.DEBUG) Log.d(TAG, e.getMessage());
    }
    data_values.clear();
  }
  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {

    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) {
      Aware.setSetting(this, Aware_Preferences.FREQUENCY_ACCELEROMETER, SAMPLING_RATE);
    }

    if (SAMPLING_RATE
        != Integer.parseInt(
            Aware.getSetting(
                getApplicationContext(),
                Aware_Preferences.FREQUENCY_ACCELEROMETER))) { // changed parameters
      SAMPLING_RATE =
          Integer.parseInt(
              Aware.getSetting(getApplicationContext(), Aware_Preferences.FREQUENCY_ACCELEROMETER));
      sensorHandler.removeCallbacksAndMessages(null);
      mSensorManager.unregisterListener(this, mAccelerometer);
      mSensorManager.registerListener(this, mAccelerometer, SAMPLING_RATE, sensorHandler);
    }

    if (Aware.DEBUG)
      Log.d(TAG, "Accelerometer service active at " + SAMPLING_RATE + " microseconds...");

    return START_STICKY;
  }
  @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!");
  }