如何获取Android指南针读数?
既然传感器_方向已被弃用,那么获取指南针航向的最佳实践是什么?旧方法非常简单。是自API级别3以来使用的标准API调用。以下是获取指南针航向并将其显示在文本视图中的基本示例。它通过实现SensorEventListener接口来实现。您可以通过更改以下代码行中的常数(即“msSensorManager.registerListener(this,mCompass,SensorManager.SENSOR_DELAY_NORMAL)”)来更改事件发送到系统的速率(请参阅OnResume()事件);然而,该设置只是对系统的一个建议。本例还使用onReuse()和onPause()方法,通过在不使用时注册和注销侦听器来延长电池寿命。希望这有帮助如何获取Android指南针读数?,android,Android,既然传感器_方向已被弃用,那么获取指南针航向的最佳实践是什么?旧方法非常简单。是自API级别3以来使用的标准API调用。以下是获取指南针航向并将其显示在文本视图中的基本示例。它通过实现SensorEventListener接口来实现。您可以通过更改以下代码行中的常数(即“msSensorManager.registerListener(this,mCompass,SensorManager.SENSOR_DELAY_NORMAL)”)来更改事件发送到系统的速率(请参阅OnResume()事件);
package edu.uw.android.thorm.wayfinder;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
public class CSensorActivity extends Activity implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mCompass;
private TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layoutsensor);
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
mCompass = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
mTextView = (TextView) findViewById(R.id.tvSensor);
}
// The following method is required by the SensorEventListener interface;
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
// The following method is required by the SensorEventListener interface;
// Hook this event to process updates;
public void onSensorChanged(SensorEvent event) {
float azimuth = Math.round(event.values[0]);
// The other values provided are:
// float pitch = event.values[1];
// float roll = event.values[2];
mTextView.setText("Azimuth: " + Float.toString(azimuth));
}
@Override
protected void onPause() {
// Unregister the listener on the onPause() event to preserve battery life;
super.onPause();
mSensorManager.unregisterListener(this);
}
@Override
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mCompass, SensorManager.SENSOR_DELAY_NORMAL);
}
}
以下是关联的XML文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tvSensor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
是的,我在文档中找到了。还有两个问题:您将R和值初始化为什么?还有,有没有一种方法可以获取通知,或者整个工作模型都已弃用?好的,在源代码库中找到了一个很好的示例。我不会在这里复制它,但是如果你浏览git Android存储库,看看开发的结尾/samples/Compass/src/com/example/Android/CompassActivity.javaNotifications/events是一种不好的传感器处理方式,这就是为什么afaik不推荐它的原因。每一个细微的方面更改都会触发大量事件,基本上会使UI线程被数据阻塞。@CodeFusionMobile您所说的有意义,但这在框架中还没有很好地实现:获取适当的矩阵来调用getOrientation(…)
,您需要同时注册到传感器。键入加速计和传感器。键入磁场事件。。。或者至少上面提供的示例代码中显示了这一点。这使得两个传感器注册,而不是一个!这是否仍然使用OP所指的不推荐使用的方向传感器?