Пример #1
0
  @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!");
  }
Пример #2
0
  @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;
  }