my code stock.com

Here's the GravitySensor class, which uses SensorManager class.

How is helping me?

This class helps by providing (x,y,z) coords from the accelerator sensor.

Snippet options

Download: Download snippet as gravitysensorjava.java.
Copy snippet: For this you need a free my code stock.com account.
Embed code : You will find the embed code for this snippet at the end of the page, if you want to embed it into a website or a blog!

package com.example.bluwif;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

public class GravitySensor {
	
	SensorManager mSensorManager;
	  float mAccel; // acceleration apart from gravity
	  float mAccelCurrent; // current acceleration including gravity
	  float mAccelLast; // last acceleration including gravity

	  final SensorEventListener mSensorListener = new SensorEventListener() {

	    public void onSensorChanged(SensorEvent se) {
	      float x = se.values[0];
	      float y = se.values[1];
	      float z = se.values[2];
	      mAccelLast = mAccelCurrent;
	      mAccelCurrent = (float) Math.sqrt((double) (x*x + y*y + z*z));
	      float delta = mAccelCurrent - mAccelLast;
	      mAccel = mAccel * 0.9f + delta; // perform low-cut filter
	    }

	    public void onAccuracyChanged(Sensor sensor, int accuracy) {
	    }
	  };


}

Create a free my code stock.com account now.

my code stok.com is a free service, which allows you to save and manage code snippes of any kind and programming language. We provide many advantages for your daily work with code-snippets, also for your teamwork. Give it a try!

Find out more and register now

You can customize the height of iFrame-Codes as needed! You can find more infos in our API Reference for iframe Embeds.