Back to project page android-wear-go-walk.
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.
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) { } }