Reputation: 2211
I want to get the values of the gyroscope and accelerometer in the phone. The following is my code
public class MainActivity extends Activity implements SensorEventListener{
private SensorManager sensorManager;
private long lastUpdate;
private GPSTracker gpss;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
lastUpdate = System.currentTimeMillis();
}
@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);
return true;
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER && event.sensor.getType() == Sensor.TYPE_GRAVITY) {
getAccelerometer(event);
}
}
private void getAccelerometer(SensorEvent event) {
// TODO Auto-generated method stub
float[] values = event.values;
// Movement
float x = values[0];
float y = values[1];
float z = values[2];
float accelationSquareRoot = (x * x + y * y + z * z)
/ (SensorManager.GRAVITY_EARTH * SensorManager.GRAVITY_EARTH);
long actualTime = System.currentTimeMillis();
if (accelationSquareRoot >= 10) //
{
if (actualTime - lastUpdate < 200) {
return;
}
lastUpdate = actualTime;
gpss = new GPSTracker(MainActivity.this);
if(gpss.canGetLocation()){
double latitude = gpss.getLatitude();
double longitude = gpss.getLongitude();
// \n is for new line
Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
}else{
// can't get location
// GPS or Network is not enabled
// Ask user to enable GPS/network in settings
gpss.showSettingsAlert();
}
}
}
@Override
protected void onResume() {
super.onResume();
// register this class as a listener for the orientation and
// accelerometer sensors
Sensor gsensor = sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);
Sensor asensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sensorManager.registerListener(this,
gsensor,
SensorManager.SENSOR_DELAY_NORMAL);
sensorManager.registerListener(this,
asensor,
SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
// unregister listener
super.onPause();
sensorManager.unregisterListener(this);
}
}
The code works fine if I use
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
getAccelerometer(event);
}
}
But when I include the *TYPE_GRAVITY* it doesn't work. I think I am making the mistake in the getType() function. Could someone tell me the correct syntax to detect two events simultaneously?
Upvotes: 2
Views: 367
Reputation: 76888
In your code here:
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER
&& event.sensor.getType() == Sensor.TYPE_GRAVITY) {
getAccelerometer(event);
}
}
You're getting the value for the event's type and asking if it's two different things at once. It's no different than having:
int a = 1;
if (a == 1 && a == 2) { ... }
Every SensorEvent
is going to have exactly one type. If you want to call your method in either case, use ||
instead of &&
in your conditional.
If you want to act independently then you need to have two checks. switch()
is a good way to go:
@Override
public void onSensorChanged(SensorEvent event) {
switch(event.sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
getAccelerometer(event);
break;
case Sensor.TYPE_GRAVITY:
doSomethingDifferent();
break;
}
}
}
Upvotes: 2