2

Sensor.TYPE_ORIENTATION 在移动设备上运行良好,但在平板电脑上无法运行。

List<Sensor> mySensors = mySensorManager.getSensorList(Sensor.TYPE_ORIENTATION);

if(mySensors.size() > 0)
{
  Toast.makeText(this, "Start ORIENTATION Sensor", Toast.LENGTH_LONG).show();
}
else
{
  Toast.makeText(this, "No ORIENTATION Sensor", Toast.LENGTH_LONG).show();
}

谁能帮我。

4

2 回答 2

1

看起来Sensor.TYPE_ORIENTATION常量已被弃用。改为使用SensorManager.getOrientation()

于 2012-09-26T14:49:36.827 回答
0

我使用 SensorManager.getOrientation() 它也可以在手机中使用,但不能在平板电脑中使用。

float[] mGravity;
float[] mGeomagnetic;
float RR[] = new float[9];
float I[] = new float[9];
float orientation[] = new float[3];

SensorManager mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
Sensor mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
Sensor mMageneticField = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);

mSensorManager.registerListener(this,mAccelerometer,SensorManager.SENSOR_DELAY_NORMAL);
mSensorManager.registerListener(this,mMageneticField,SensorManager.SENSOR_DELAY_NORMAL);

// onSensorChanged 内部

if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
{   
mGravity = event.values.clone();    
}
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
{
    mGeomagnetic = event.values.clone();
}

SensorManager.getRotationMatrix(RR, I, mGravity, mGeomagnetic);
SensorManager.getOrientation(RR, orientation);
azimuth = orientation[0];
pitch = orientation[1];
roll = orientation[2];
System.out.println("Pitch " + pitch);
于 2012-10-19T10:04:55.710 回答