Hatte gedacht das wäre überflüssig
Die komplette Main.java
package de.pirate4888.gleitschirmvario3;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class Main extends Activity {
TextView error;
TextView magnetVanzeige;
TextView gravityVanzeige;
Sensoren magnetfield = new Sensoren("magnetfield");
Sensoren gravityfield= new Sensoren("gravityfield");
float[] magvektor= new float[3];
float[] gravvektor= new float[3];
boolean errorflag = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
error=(TextView)findViewById(R.id.error);
magnetVanzeige=(TextView)findViewById(R.id.magnetVanzeige);
gravityVanzeige=(TextView)findViewById(R.id.gravityVanzeige);
errorflag=magnetfield.setUpSensor();
if (errorflag=true) error.setText("Error: Sensor nicht konfigurierbar");
magvektor=magnetfield.getVektor();
magnetfield.setDownSensor();
magnetVanzeige.setText("Magnetfeld Vektor:"+
"\nX:"+ magvektor[0]+
"\nY:"+ magvektor[1]+
"\nZ:"+ magvektor[2]);
errorflag=gravityfield.setUpSensor();
if (errorflag=true) error.setText("Error: Sensor nicht konfigurierbar");
gravvektor=gravityfield.getVektor();
gravityfield.setDownSensor();
gravityVanzeige.setText("Gravitations Vektor:"+
"\nX:"+ gravvektor[0]+
"\nY:"+ gravvektor[1]+
"\nZ:"+ gravvektor[2]);
return true;
}
}
Die Komplette Sensoren.java
package de.pirate4888.gleitschirmvario3;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
public class Sensoren extends Activity implements SensorEventListener {
SensorManager sensorManager;
private String type;
private float vektor[]=new float[3];
public Sensoren(String t){
type = t;
}
public boolean setUpSensor(){
if (type=="accelerometer"){
Sensor gravityfield;
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
gravityfield=sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sensorManager.registerListener(this, gravityfield, SensorManager.SENSOR_DELAY_NORMAL);
return false;
}
else if (type=="magnetfield"){
Sensor magnetfield;
sensorManager=(SensorManager) getSystemService(SENSOR_SERVICE);
magnetfield=sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
sensorManager.registerListener(this, magnetfield, SensorManager.SENSOR_DELAY_NORMAL);
return false;
}
else return true;
}
public void setDownSensor(){
sensorManager.unregisterListener(this);
}
public float[] getVektor(){
return vektor;
}
@Override public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
vektor=event.values;
}
}