, , - , SensorListener. , SensorEvent , SensorListener.onSensorChanged(SensorEvent event), SensorListener :
public class ShakeDetector implements SensorEventListener {
@Override
public void onSensorChanged(SensorEvent event) {
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
onSensorUpdate(x, y, z);
}
public void onSensorUpdate(float x, float y, float z) {
}
}
Then I can call onSensorUpdateddirectly from my test code, which simulates shooting with an accelerometer.
private void simulateShake(final float amplitude, int interval, int duration) throws InterruptedException {
final SignInFragment.ShakeDetector shaker = getFragment().getShakeSensorForTesting();
long start = System.currentTimeMillis();
do {
getInstrumentation().runOnMainSync(new Runnable() {
@Override
public void run() {
shaker.onSensorUpdate(amplitude, amplitude, amplitude);
}
});
Thread.sleep(interval);
} while (System.currentTimeMillis() - start < duration);
}
source
share