[Java for Android App Development] spelaben:
Hi all, I am a bit further with my project since I found some time to work on it again.

//edit, force closes on starting fixed!

Here is what it is supposed to be doing: It reads the x, y and z values for acceleration from the LINEAR_ACCELERATION sensor. If a certain value is exceeded a timer will be started. If the amount of seconds the timer counts is higher than a defined threshold, it will play the default notifcation sound.

If the value from the sensor is below the threshold, it will stop the timer. The whole timer thing isn't working, the notification is played as soon as the normal acceleration threshold is passed and the time is ignored.

Please review my code below and tell me what I'm not seeing here, note that I'm still a noob. :)

Also I'm interested in a method to capture the maximum values read from the sensor and put it in a textview, can you help me?

AccelMeter.java:

package com.example.accelmeter;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class AccelMeter extends Activity implements SensorEventListener {
private SensorManager sensorManager;

// Objekte deklarieren
TextView xC;
TextView yC;
TextView zC;
Chronometer chrono;

/*	accThreshold = Schwellenwert in m/s² für die Beschleunigung
* accInterval = Zeitraum in Sekunden, über den der Wert für die Beschleunigung überwacht wird
*/
private float accThreshold = 50;
private float accInterval = 0;

[MENTION=439709]override[/MENTION]
public void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_accel_meter);

// Achsen Objekte erstellen
xC=(TextView)findViewById(R.id.xCoordinate);
yC=(TextView)findViewById(R.id.yCoordinate);
zC=(TextView)findViewById(R.id.zCoordinate);

/* Sensor.TYPE_LINEAR_ACCELERATION = Beschleunigung auf jeder Achse ohne Gravitation in m/s²
* SENSOR_DELAY_FASTEST = schnellster Modus für Messung. Alternativen z.B. _GAME, _NORMAL oder _UI
*/
sensorManager=(SensorManager)getSystemService(SENSOR_SERVICE);
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION),
SensorManager.SENSOR_DELAY_NORMAL);

// Schwellenwert festlegen und mit einer Toast-Benachrichtigung bestätigen, Fehlerbehandlung wenn kein Wert eingegeben wurde
Button setThreshold = (Button)findViewById(R.id.setThreshold);
setThreshold.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
try {
EditText et = (EditText)findViewById(R.id.threshold);
accThreshold = Float.valueOf(et.getText().toString());
Toast.makeText(getApplicationContext(), "Neuer Schwellenwert festgelegt!", Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
Toast.makeText(getApplicationContext(), "Es wurde noch kein Wert eingegeben!", Toast.LENGTH_SHORT).show();
}
}
});

// Intervall festlegen und mit einer Toast-Benachrichtigung bestätigen, Fehlerbehandlung wenn kein Wert eingegeben wurde
Button setInterval = (Button)findViewById(R.id.setInterval);
setInterval.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
try {
EditText et = (EditText)findViewById(R.id.editInterval);
accInterval = Float.valueOf(et.getText().toString());
Toast.makeText(getApplicationContext(), "Neues Intervall festgelegt!", Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
Toast.makeText(getApplicationContext(), "Es wurde noch kein Wert eingegeben!", Toast.LENGTH_SHORT).show();
}
}
});

// Programm beenden
Button exit = (Button)findViewById(R.id.exit);
exit.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
finish();
System.exit(0);
}
});
}

public void onSensorChanged(SensorEvent event){

// Sensor Typ überprüfen
if(event.sensor.getType()==Sensor.TYPE_LINEAR_ACCELERATION){

// Richtungen verknüpfen
float x=event.values[0];
float y=event.values[1];
float z=event.values[2];

// Gerundete Ergebnisse mit vier Nachkommastellen ausgeben
xC.setText("X: "+(Math.abs(Math.round(x * 1000) / 1000.0)));
yC.setText("Y: "+(Math.abs(Math.round(y * 1000) / 1000.0)));
zC.setText("Z: "+(Math.abs(Math.round(z * 1000) / 1000.0)));

// Wenn Beschleunigung über Schwellenwert liegt, starte Timer. Wenn Timer das festgelete Intervall überschreitet, spiele Benachrichtigungston
if (x + y + z > accThreshold)
{
chrono = new Chronometer(this);
chrono.setBase(SystemClock.elapsedRealtime());
chrono.start();
long seconds = ((chrono.getBase()-SystemClock.elapsedRealtime()/1000%60));
if (seconds >= accInterval)
{
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();
}
else
{
chrono.stop();
}

}
}
}

[MENTION=439709]override[/MENTION]
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}

}

activity_accel_meter.xml:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >

<TableRow tools:ignore="ExtraText" >

<TextView
android:id="@+id/xCoordinate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="X Koordinate: "
android:textAppearance="?android:attr/textAppearanceLarge"
tools:ignore="HardcodedText" />
/>

</TableRow>

<TableRow>

<TextView
android:id="@+id/yCoordinate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Y Koordinate: "
android:textAppearance="?android:attr/textAppearanceLarge"
tools:ignore="HardcodedText" />

/>

</TableRow>

<TableRow>

<TextView
android:id="@+id/zCoordinate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Z Koordinate: "
android:textAppearance="?android:attr/textAppearanceLarge"
tools:ignore="HardcodedText" />

/>

</TableRow>

<EditText
android:id="@+id/threshold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Schwellenwert (float)"
android:inputType="numberDecimal"
tools:ignore="HardcodedText" />

<Button
android:id="@+id/setThreshold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Schwellenwert festlegen"
tools:ignore="HardcodedText" />

<EditText
android:id="@+id/editInterval"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Zeit in Sekunden"
android:inputType="number"
tools:ignore="HardcodedText" >

<requestFocus />
</EditText>

<Button
android:id="@+id/setInterval"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Zeitraum für Überwachung festlegen"
tools:ignore="HardcodedText" />

<Button
android:id="@+id/exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Beenden"
tools:ignore="HardcodedText" />

</TableLayout>