1
public class MainActivity extends Activity implements SensorEventListener {
    final String tag = "myLogs";
    SensorManager sm = null;
    Sensor lightSensor;
    float lightQuantity;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        sm = (SensorManager) getSystemService(SENSOR_SERVICE);
        lightSensor = sm.getDefaultSensor(Sensor.TYPE_LIGHT);

        if(lightSensor == null)
            Log.d(tag, "no sensor:(");
        else
            Log.d(tag, "GOT IT!");

    }

    @Override
    protected void onResume() {
        super.onResume();
        sm.registerListener((SensorEventListener)this, lightSensor, SensorManager.SENSOR_DELAY_NORMAL);
    }

    @Override
    protected void onStop() {
        sm.unregisterListener((SensorEventListener)this);
        super.onStop();
    }

    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        Log.d(tag,"onAccuracyChanged: " + sensor.getType() + ", accuracy: " + accuracy);

    }

    public void onSensorChanged(SensorEvent event) {
        lightQuantity = event.values[0];
        Log.d(tag,"onSensorChanged: " + event.sensor.getType() + ", result: " + lightQuantity);

    }
}

我无法使用此代码获取光传感器统计信息。“无传感器:(”消息始终。getSensorList()方法也不显示光传感器。我的设备:Xperia PRO(mk16i)。SE光传感器测试效果很好

4

1 回答 1

0

问题是 Xperia 没有通过驱动程序以标准 Android 方式实现传感器。只有一个专有的实现可以通过读取内核变量来使用,如下所述。(C)

于 2012-12-05T21:06:47.787 回答