我想要实现的是在 method 中返回正确的结果集isClose
。问题是该isClose
方法没有等待onSensorChanged
被触发并返回字段的默认“0”值isClose
。
我这样称呼我的职位类:
Position mPosition = new Position();
boolean result = mPosition.isInPocket(this);
职位类别:
public class Position implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mProximity;
private boolean isClose;
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
public void onSensorChanged(SensorEvent event) {
float[] value = event.values;
if(value[0] > 0) {
isClose = false;
} else
{
isClose = true;
}
}
public boolean isClose(Context context) {
mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
mProximity = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
mSensorManager.registerListener(this, mProximity, 0);
return isClose; // I'd like to return this with value set in onSensorChanged.
}
}