1
I need to take the slope and declination value of the cell phone in landscape mode, but the values I need are in decimal degrees, from -90 to 90 degrees. I tried to use the accelerometer, but the values of this sensor are 0 a 10. I also tried to use the magnetometer, but some phones do not have this sensor. How can I get these values using only the accelerometer?
Code using the accelerometer:
package br.eng.itech.smarthipsometer_01;
import androidx.appcompat.app.AppCompatActivity;
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 android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity implements SensorEventListener {
SensorManager sensorManager;
Sensor accelerometer;
TextView textViewX;
TextView textViewY;
TextView textViewZ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textViewX = (TextView) findViewById(R.id.textViewX);
textViewY = (TextView) findViewById(R.id.textViewY);
textViewZ = (TextView) findViewById(R.id.textViewZ);
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}
@Override
protected void onResume() {
super.onResume();
if (accelerometer == null){
Toast.makeText(this, "Sensor acelerômetro não encontrado no dispositivo", Toast.LENGTH_SHORT).show();
finish();
}else {
sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
}
@Override
protected void onPause() {
sensorManager.unregisterListener(this);
super.onPause();
}
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
textViewX.setText(String.format("X: %.2f", sensorEvent.values[0]));
textViewY.setText(String.format("Y: %.2f", sensorEvent.values[1]));
textViewZ.setText(String.format("Z: %.2f", sensorEvent.values[2]));
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
}
thank you so much for the help! Answer well summarized but very well explained. I managed to reach my goal through your explanation.
– RDamazio