Browse Source

added Beschleunigungssensor

lm
Leon Market 1 year ago
parent
commit
620528302e

+ 86
- 0
app/src/main/java/com/example/ueberwachungssystem/Beschleunigungssensor.java View File

@@ -0,0 +1,86 @@
package com.example.ueberwachungssystem;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.TextView;
import com.example.ueberwachungssystem.Logger;

public class Beschleunigungssensor extends AppCompatActivity implements SensorEventListener
{

private Logger logger;
private SensorManager sensorManager;
private int sensorType = Sensor.TYPE_ACCELEROMETER;
private Sensor sensor;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setTitle(this.getClass().getSimpleName());
TextView textView = new TextView(this);
setContentView(textView);

logger = new Logger(this.getClass().getSimpleName(),textView,"");

sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
if(sensorManager.getSensorList(sensorType).size()==0)
{
logger.log("Es gibt den gewünschten Sensor nicht");
sensor = null;
}
else
{
sensor = sensorManager.getSensorList(sensorType).get(0);
}
}

@Override
protected void onResume()
{
super.onResume();
if(sensor != null)
{
if(sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_GAME))
{
logger.log("Wir haben uns beim Sensor angemeldet.");
}
else
{
logger.log("Das Anmelden beim Sensor hat nicht so gut geklappt");
}
}
}

@Override
protected void onPause()
{
super.onPause();
if(sensor != null) {
sensorManager.unregisterListener(this, sensor);
logger.log("Wir haben uns beim Sensor abgemeldet");
}
}

@Override
public void onSensorChanged(SensorEvent event)
{
StringBuilder sb = new StringBuilder();
sb.append("t=").append(event.timestamp)
.append("\nx=").append(event.values[0])
.append("\ny=").append(event.values[1])
.append("\nz=").append(event.values[2]);
logger.clearLog();
logger.log(sb.toString());
}

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

Loading…
Cancel
Save