Skip to content
Snippets Groups Projects
Accelerometer.java 4.36 KiB
Newer Older
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.hardware.TriggerEvent;
import android.hardware.TriggerEventListener;
import android.widget.Toast;

import com.example.pharmacy.frontEnd.R;
public class Accelerometer extends Service implements StimulusStrategy, SensorEventListener {
    public int onStartCommand(Intent intent, int flags, int startId) {
        mSensorManager = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE);
        mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), 60000000);

        //Sampling period is still far too often here.
        //Slightly better when app is not in foreground
        am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
        setUpClock();
        SharedPreferences shared = this.getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE);
        Intent i = new Intent(getBaseContext(), com.example.stimulus.SedentaryStimulus.class);
        i.putExtras(new Intent(this, this.getClass()));
       // i.putExtra(getString(R.string.serviceID), startId);
        PendingIntent pi = PendingIntent.getBroadcast(getBaseContext(), R.integer.alarm_rc, i, PendingIntent.FLAG_UPDATE_CURRENT);
        am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+3000, pi);
       // int interval = shared.getInt(getString(R.string.notify_me), R.integer.notify_period_minutes);
        //am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+(1000*60*interval), pi);
        Toast saved = Toast.makeText(this, R.string.motion,Toast.LENGTH_LONG);
        saved.show();
        Intent i = new Intent(getBaseContext(), com.example.stimulus.SedentaryStimulus.class);
        PendingIntent pi = PendingIntent.getBroadcast(getBaseContext(), R.integer.alarm_rc, i, PendingIntent.FLAG_UPDATE_CURRENT);
        am.cancel(pi);
    private void stepCounter(){
        Intent intent = new Intent(this, StepCounter.class);
        startService(intent);
    }
        //Needs major tuning currently vibration triggers motion
        //Need to work out the maths on this
        //Google API Activity Detection client??
        //Maybe work maths out and use this as 3rd option
        //2 Manual options and an existing option
        float[] gravity = new float[3];
        float[] linearAcc = new float[3];

        final float alpha = (float) 0.8;

        gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0];
        gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1];
        gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2];

        linearAcc[0] = event.values[0] - gravity[0];
        linearAcc[1] = event.values[1] - gravity[1];
        linearAcc[2] = event.values[2] - gravity[2];
        Log.d("ACCEL", "SENSOR EVENT");
        for(int i = 0; i < 3; i++){
    @Override
    public void onAccuracyChanged(Sensor sensor, int i) {

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