Skip to content

Instantly share code, notes, and snippets.

@kleino
Created November 6, 2013 11:27
Show Gist options
  • Save kleino/7334598 to your computer and use it in GitHub Desktop.
Save kleino/7334598 to your computer and use it in GitHub Desktop.
Android Accelerometer
package com.example.SturzSensor;
import android.app.Activity;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.GraphViewSeries;
import com.jjoe64.graphview.LineGraphView;
public class Main extends Activity implements SensorEventListener {
private SensorManager sensorManager;
private boolean color = false;
private View view;
private long lastUpdate;
public final String TAG = "MAIN";
private Double[] dataPoints;
private GraphView graphView;
private GraphViewSeries series;
private double graph2LastXValue = 5d;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
view = findViewById(R.id.textView);
view.setBackgroundColor(Color.GREEN);
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
lastUpdate = System.currentTimeMillis();
series = new GraphViewSeries(new GraphView.GraphViewData[] {
});
graphView = new LineGraphView(
this // context
, "Acceleration" // heading
);
graphView.addSeries(series); // data
graphView.setViewPort(1, 10);
graphView.setManualYAxisBounds(20,1);
graphView.setScalable(true);
LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);
layout.addView(graphView);
dataPoints = new Double[500];
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
getAccelerometer(event);
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
//To change body of implemented methods use File | Settings | File Templates.
}
private void getAccelerometer(SensorEvent event) {
float[] values = event.values;
// Movement
float x = values[0];
float y = values[1];
float z = values[2];
//Log.d(TAG, "x: " + x + " y: " + y + " z: " + z);
float accelationSquareRoot = (x * x + y * y + z * z)
/ (SensorManager.GRAVITY_EARTH * SensorManager.GRAVITY_EARTH);
double acceleration = Math.sqrt(accelationSquareRoot);
long actualTime = System.currentTimeMillis();
graph2LastXValue += 1d;
series.appendData(new GraphView.GraphViewData(graph2LastXValue, acceleration), true, 10);
addDataPoint(acceleration);
//Log.d(TAG, "Beschleunigung: " + accelationSquareRoot);
/*
if (accelationSquareRoot >= 2) //
{
if (actualTime - lastUpdate < 200) {
return;
}
lastUpdate = actualTime;
Toast.makeText(this, "Device was shuffed", Toast.LENGTH_SHORT)
.show();
if (color) {
view.setBackgroundColor(Color.GREEN);
} else {
view.setBackgroundColor(Color.RED);
}
color = !color;
}
*/
}
private void addDataPoint(double acceleration) {
dataPoints[499] = acceleration;
//To change body of created methods use File | Settings | File Templates.
}
@Override
protected void onResume() {
super.onResume();
// register this class as a listener for the orientation and
// accelerometer sensors
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
// unregister listener
super.onPause();
sensorManager.unregisterListener(this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment