use of android.hardware.SensorEvent in project robolectric by robolectric.
the class ShadowSensorManagerTest method shouldSendSensorEventToMultipleRegisteredListeners.
@Test
public void shouldSendSensorEventToMultipleRegisteredListeners() {
TestSensorEventListener listener1 = new TestSensorEventListener();
TestSensorEventListener listener2 = new TestSensorEventListener();
Sensor sensor = sensorManager.getDefaultSensor(SensorManager.SENSOR_ACCELEROMETER);
sensorManager.registerListener(listener1, sensor, SensorManager.SENSOR_DELAY_NORMAL);
sensorManager.registerListener(listener2, sensor, SensorManager.SENSOR_DELAY_NORMAL);
SensorEvent event = shadow.createSensorEvent();
shadow.sendSensorEventToListeners(event);
assertThat(listener1.getLatestSensorEvent().get()).isEqualTo(event);
assertThat(listener2.getLatestSensorEvent().get()).isEqualTo(event);
}
use of android.hardware.SensorEvent in project android_packages_apps_CMParts by LineageOS.
the class KeyHandler method processEvent.
private void processEvent(final int action) {
mProximityWakeLock.acquire();
mSensorManager.registerListener(new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
mProximityWakeLock.release();
mSensorManager.unregisterListener(this);
if (!mEventHandler.hasMessages(GESTURE_REQUEST)) {
// The sensor took too long; ignoring
return;
}
mEventHandler.removeMessages(GESTURE_REQUEST);
if (event.values[0] == mProximitySensor.getMaximumRange()) {
Message msg = getMessageForAction(action);
mEventHandler.sendMessage(msg);
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// Ignore
}
}, mProximitySensor, SensorManager.SENSOR_DELAY_FASTEST);
}
use of android.hardware.SensorEvent in project AndroidLife by CaMnter.
the class SensorManagerActivity method initData.
/**
* Initialize the Activity data
*/
@Override
protected void initData() {
// 获取传感器管理器
SensorManager sensorManager = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE);
// 获取默认加速度传感器
Sensor linear = sensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);
/**
* 传感器监听
* SensorEvent.values[0] = x
* SensorEvent.values[1] = y
* SensorEvent.values[2] = z
*/
SensorEventListener listener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
String info = "";
for (int i = 0; i < event.values.length; i++) {
info += "event.values[" + i + "]:" + event.values[i] + "\t";
}
Log.i("SensorManagerActivity", info);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
// 给对应传感器添加监听
sensorManager.registerListener(listener, linear, SensorManager.SENSOR_DELAY_NORMAL);
// 获得全部的传感器列表
List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ALL);
this.sensorManagerTV.setText(sensors.size() + "");
for (Sensor sensor : sensors) {
TextView title = new TextView(this);
title.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
title.setTextSize(15);
title.setTextColor(this.getResources().getColor(R.color.themeColor));
switch(sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
{
title.setText("1.加速度传感器" + "(" + sensor.getName() + ")");
this.addInfoView(sensor, title);
break;
}
case Sensor.TYPE_MAGNETIC_FIELD:
{
title.setText("2.磁力传感器" + "(" + sensor.getName() + ")");
this.addInfoView(sensor, title);
break;
}
case Sensor.TYPE_ORIENTATION:
{
title.setText("3.方向传感器" + "(" + sensor.getName() + ")");
this.addInfoView(sensor, title);
break;
}
case Sensor.TYPE_GYROSCOPE:
{
title.setText("4.陀螺仪传感器" + "(" + sensor.getName() + ")");
this.addInfoView(sensor, title);
break;
}
case Sensor.TYPE_LIGHT:
{
title.setText("5.光线感应传感器" + "(" + sensor.getName() + ")");
this.addInfoView(sensor, title);
break;
}
case Sensor.TYPE_PRESSURE:
{
title.setText("6.压力传感器" + "(" + sensor.getName() + ")");
this.addInfoView(sensor, title);
break;
}
case Sensor.TYPE_TEMPERATURE:
{
title.setText("7.温度传感器" + "(" + sensor.getName() + ")");
this.addInfoView(sensor, title);
break;
}
case Sensor.TYPE_PROXIMITY:
{
title.setText("8.距离传感器" + "(" + sensor.getName() + ")");
this.addInfoView(sensor, title);
break;
}
case Sensor.TYPE_GRAVITY:
{
title.setText("9.重力传感器" + "(" + sensor.getName() + ")");
this.addInfoView(sensor, title);
break;
}
case Sensor.TYPE_LINEAR_ACCELERATION:
{
title.setText("10.线性加速度传感器" + "(" + sensor.getName() + ")");
this.addInfoView(sensor, title);
break;
}
case Sensor.TYPE_ROTATION_VECTOR:
{
title.setText("11.旋转矢量传感器" + "(" + sensor.getName() + ")");
this.addInfoView(sensor, title);
break;
}
}
}
}
Aggregations