Android:Orientation Sensor无法在平板电脑上运行

时间:2012-09-26 14:20:46

标签: android

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();
}

任何人都可以帮助我。

2 个答案:

答案 0 :(得分:1)

看起来像Sensor.TYPE_ORIENTATION常数is deprecated。请改用SensorManager.getOrientation()

答案 1 :(得分: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);