如何获取安卓手机的指南针读数?

10

现在SENSOR_ORIENTATION已被弃用,那么获取指南针方向的最佳实践是什么?旧的方式非常简单。

2个回答

8
以下是一个基本示例,用于获取指南针方向并在TextView中显示。它通过实现SensorEventListener接口来实现。您可以通过更改以下代码行中的常量(即“mSensorManager.registerListener(this,mCompass,SensorManager.SENSOR_DELAY_NORMAL);”)(请参见OnResume()事件)来更改将事件传递到系统的速率;但是,该设置仅是对系统的建议。此示例还使用onReuse()和onPause()方法,在不使用时通过注册和取消注册Listener来保留电池寿命。希望这能帮助你。
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>

4
这是否仍在使用 OP 提到的已弃用的方向传感器? - Tim

7

是的,我在文档中找到了这个。还有两个问题:你将R和values初始化为什么值?此外,是否有一种方式来获取通知,或者整个工作模型已经弃用了? - Edward Falk
2
好的,在源代码库中找到了一个很好的例子。我不会在这里复制它,但如果你浏览Android的git存储库,看一下development/samples/Compass/src/com/example/android/compass/CompassActivity.java的末尾。 - Edward Falk
1
通知/事件是传感器的一种不好的方式,这就是为什么它已经被弃用了。每个微小的方面变化都会触发大量的事件,从本质上说,用数据淹没UI线程。 - CodeFusionMobile
@CodeFusionMobile 你说的很有道理,但这在框架中尚未得到很好的实现:为了获取适当的矩阵来调用getOrientation(...),您需要同时注册Sensor.TYPE_ACCELEROMETERSensor.TYPE_MAGNETIC_FIELD事件……或者至少在上面提供的示例代码中是这样显示的。这意味着需要注册两个传感器而不是一个! - JM Lord

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接