Search in sources :

Example 16 with SensorEvent

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);
}
Also used : SensorEvent(android.hardware.SensorEvent) Sensor(android.hardware.Sensor) Test(org.junit.Test)

Example 17 with SensorEvent

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);
}
Also used : Message(android.os.Message) SensorEventListener(android.hardware.SensorEventListener) SensorEvent(android.hardware.SensorEvent) Sensor(android.hardware.Sensor)

Example 18 with SensorEvent

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;
                }
        }
    }
}
Also used : SensorManager(android.hardware.SensorManager) ViewGroup(android.view.ViewGroup) SensorEventListener(android.hardware.SensorEventListener) TextView(android.widget.TextView) SensorEvent(android.hardware.SensorEvent) Sensor(android.hardware.Sensor)

Aggregations

SensorEvent (android.hardware.SensorEvent)18 Sensor (android.hardware.Sensor)11 SensorEventListener (android.hardware.SensorEventListener)8 Test (org.junit.Test)6 TextView (android.widget.TextView)3 View (android.view.View)2 AlertDialog (android.app.AlertDialog)1 DialogInterface (android.content.DialogInterface)1 Intent (android.content.Intent)1 SensorManager (android.hardware.SensorManager)1 Message (android.os.Message)1 ViewGroup (android.view.ViewGroup)1 OnProximityListener (com.example.android.recipe071.ProximityManager.OnProximityListener)1 LangTestActivity (com.example.test.andlang.test.LangTestActivity)1 ShakeSensorUtil (com.example.test.andlang.util.ShakeSensorUtil)1 CircularProgressView (com.github.rahatarmanahmed.cpv.CircularProgressView)1 Field (java.lang.reflect.Field)1 ClassParameter (org.robolectric.util.ReflectionHelpers.ClassParameter)1