Android Open Source - android-wear-go-walk Step Checker






From Project

Back to project page android-wear-go-walk.

License

The source code is released under:

MIT License

If you think the Android project android-wear-go-walk listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.beltranfebrer.gowalk2;
/*w w  w  .j  ava  2s.  co  m*/
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.Log;

/**
 * Created by Miquel on 15/07/2014.
 */
public class StepChecker implements SensorEventListener {

    private static final String TAG = SensorEventListener.class.toString();
    private static float previousSteps;
    private Context context;

    public StepChecker(Context context) {
        this.context = context;
        SensorManager sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
        Sensor stepCounter = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
        if (stepCounter != null) {
            sensorManager.registerListener(this, stepCounter, SensorManager.SENSOR_DELAY_NORMAL);
            Log.d(TAG, "Step checker created");
        } else {
            // Not available
            Log.e(TAG, "Step checker will not work on this device!");
        }
    }

    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {
        float steps = sensorEvent.values[0];
        Log.d(TAG, "Go walk! Steps: " + steps + " Diff: " + (steps - previousSteps));
        //if (steps - previousSteps < 100) {
            Intent startIntent = new Intent(context, WearActivity.class);
            startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startIntent.putExtra(WearActivity.EXTRA_STEPS, steps - previousSteps);
            context.startActivity(startIntent);
        //}
        previousSteps = steps;
        UnregisterListeners();
    }

    void UnregisterListeners() {
        SensorManager sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
        sensorManager.unregisterListener(this);
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int i) {

    }
}




Java Source Code List

com.beltranfebrer.gowalk2.ApplicationTest.java
com.beltranfebrer.gowalk2.BootCompletedIntentReceiver.java
com.beltranfebrer.gowalk2.MainActivity.java
com.beltranfebrer.gowalk2.StepChecker.java
com.beltranfebrer.gowalk2.WearActivity.java
com.beltranfebrer.gowalk2.WearService.java