gpt4 book ai didi

Android SensorListener 弃用

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:10:38 26 4
gpt4 key购买 nike

我正在尝试更改以下代码以使用指南针,但是我遇到了麻烦,我尝试使用 SensorEventListener,但我失败了。有没有我可以遵循的教程?任何帮助将不胜感激。

private static SensorListener orientationListener;
private static boolean isTrackingOrientation = false;

@Override
public void onCreate(Bundle icicle) {

super.onCreate(icicle);
setContentView(R.layout.main);

orientationListener = new SensorListener() {
public void onSensorChanged(int s, float v[]) {
float northDirection = v[android.hardware.SensorManager.DATA_X];
((QiblaCompassView)findViewById(R.id.qibla_compass)).setDirections(northDirection, VARIABLE.qiblaDirection);

}
public void onAccuracyChanged(int s, int a) {
}
};
}

@Override
public void onResume() {
startTrackingOrientation();
super.onResume();
}
@Override
public void onPause() {
stopTrackingOrientation();
super.onPause();
}

private void startTrackingOrientation() {
if(!isTrackingOrientation) isTrackingOrientation = ((SensorManager)getSystemService(SENSOR_SERVICE)).registerListener(orientationListener, android.hardware.SensorManager.SENSOR_ORIENTATION);
}
private void stopTrackingOrientation() {
if(isTrackingOrientation) ((SensorManager)getSystemService(SENSOR_SERVICE)).unregisterListener(orientationListener);
isTrackingOrientation = false;
}

最佳答案

这是对传感器工作原理的一个很好的解释: http://developer.android.com/reference/android/hardware/SensorEvent.html

此代码成功运行:

package com.example.testvideo;

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.util.Log;

public class MainActivity extends Activity implements SensorEventListener {

SensorManager mSensorManager;
Sensor accSensor;
Sensor magnetSensor;

float gravity[];
float geoMagnetic[];
float azimut;
float pitch;
float roll;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
accSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
magnetSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);

mSensorManager.registerListener(this, accSensor, SensorManager.SENSOR_DELAY_NORMAL);
mSensorManager.registerListener(this, magnetSensor, SensorManager.SENSOR_DELAY_NORMAL);
}

@Override
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this, accSensor);
mSensorManager.unregisterListener(this, magnetSensor);
}

@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {

}

@Override
public void onSensorChanged(SensorEvent event) {

if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
gravity = event.values.clone();
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
geoMagnetic = event.values.clone();

if (gravity != null && geoMagnetic != null) {

float R[] = new float[9];
float I[] = new float[9];
boolean success = SensorManager.getRotationMatrix(R, I, gravity, geoMagnetic);
if (success) {
float orientation[] = new float[3];
SensorManager.getOrientation(R, orientation);
azimut = 57.29578F * orientation[0];
pitch = 57.29578F * orientation[1];
roll = 57.29578F * orientation[2];

float dist = Math.abs((float) (1.4f * Math.tan(pitch * Math.PI / 180)));

Log.d("log", "orientation values: " + azimut + " / " + pitch + " / " + roll + " dist = " + dist);
}
}
}

}

关于Android SensorListener 弃用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16737171/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com