我目前正尝试制作Android演示版,以大约50Hz的采样率测试光照水平。但是,我每秒只能进行onSensorChanged
次回调2-3次。我的手机是xiaomi mi5s(安装了lineage os 14.1),Android 7.1。
public class MainActivity extends Activity implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mLight;
@Override
public final void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mLight = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
}
@Override
public final void onAccuracyChanged(Sensor sensor, int accuracy) {
// Do something here if sensor accuracy changes.
}
@Override
public final void onSensorChanged(SensorEvent event) {
// The light sensor returns a single value.
// Many sensors return 3 values, one for each axis.
float lux = event.values[0];
Log.i("zz", lux + "");
// Do something with this sensor value.
}
@Override
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mLight, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
}
答案 0 :(得分:0)
您可以使用SENSOR_DELAY_FASTEST而不是正常。这将使它与硬件提供可靠的读数一样快。但完全有可能光传感器不能在50赫兹下工作。它的主要用例并不需要。