要在Android设备上获取指南针方向的角度,可以使用SensorManager类和SensorEventListener接口。以下是获取指南针角度的步骤:
1.在Activity中实例化SensorManager:
private SensorManager sensorManager;
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
2.注册SensorEventListener以监听方向传感器事件:
private SensorEventListener sensorEventListener = new SensorEventListener() {
float[] gravity = new float[3];
float[] geomagnetic = new float[3];
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
System.arraycopy(event.values, 0, gravity, 0, 3);
} else if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
System.arraycopy(event.values, 0, geomagnetic, 0, 3);
}
float[] rotationMatrix = new float[9];
boolean success = SensorManager.getRotationMatrix(rotationMatrix, null, gravity, geomagnetic);
if (success) {
float[] orientation = new float[3];
SensorManager.getOrientation(rotationMatrix, orientation);
float azimuth = orientation[0];
float degrees = (float) Math.toDegrees(azimuth);
degrees = (degrees + 360) % 360; // 保证角度范围在0-360度之间
// 显示指南针角度
TextView compassTextView = findViewById(R.id.compass_text_view);
compassTextView.setText("指南针角度:" + degrees);
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// do nothing
}
};
sensorManager.registerListener(sensorEventListener,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
sensorManager.registerListener(sensorEventListener,
sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
SensorManager.SENSOR_DELAY_NORMAL);
3.在应用程序关闭时注销SensorEventListener:
@Override
protected void onPause() {
super.onPause();
sensorManager.unregisterListener(sensorEventListener);
}
完整的代码示例如下:
public class MainActivity extends AppCompatActivity {
private SensorManager sensorManager;
private SensorEventListener sensorEventListener = new SensorEventListener() {
float[] gravity = new float[3];
float[] geomagnetic = new float[3];
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
System.arraycopy(event.values, 0, gravity, 0, 3);
} else if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
System.arraycopy(event.values, 0, geomagnetic, 0, 3);
}
float[] rotationMatrix = new float[9];
boolean success = SensorManager.getRotationMatrix(rotationMatrix, null, gravity, geomagnetic);
if (success) {
float[] orientation = new float[3];
SensorManager.getOrientation(rotationMatrix, orientation);
float azimuth = orientation[0];
float degrees = (float) Math.toDegrees(azimuth);
degrees = (degrees + 360) % 360; // 保证角度范围在0-360度之间
// 显示指南针角度
TextView compassTextView = findViewById(R.id.compass_text_view);
compassTextView.setText("指南针角度:" + degrees);
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// do nothing
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 实例化SensorManager
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
// 注册SensorEventListener
sensorManager.registerListener(sensorEventListener,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
sensorManager.registerListener(sensorEventListener,
sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
super.onPause();
// 在暂停Activity时注销SensorEventListener
sensorManager.unregisterListener(sensorEventListener);
}
}