android.hardware.SensorManager类的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(10.1k)|赞(0)|评价(0)|浏览(117)

本文整理了Java中android.hardware.SensorManager类的一些代码示例,展示了SensorManager类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。SensorManager类的具体详情如下:
包路径:android.hardware.SensorManager
类名称:SensorManager

SensorManager介绍

暂无

代码示例

代码示例来源:origin: stackoverflow.com

public class OnBootReceiver extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent){
    SensorManager sManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
    sensor = sManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    sManager.registerListener(new ShakeEventListener(), sensor, SensorManager.SENSOR_DELAY_NORMAL); // or other delay
  }
}

代码示例来源:origin: journeyapps/zxing-android-embedded

public void stop() {
  if (lightSensor != null) {
    SensorManager sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
    sensorManager.unregisterListener(this);
    lightSensor = null;
  }
}

代码示例来源:origin: libgdx/libgdx

void registerSensorListeners () {
  if (config.useAccelerometer) {
    manager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
    if (manager.getSensorList(Sensor.TYPE_ACCELEROMETER).isEmpty()) {
      accelerometerAvailable = false;
    } else {
      Sensor accelerometer = manager.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
      accelerometerListener = new SensorListener();
      accelerometerAvailable = manager.registerListener(accelerometerListener, accelerometer,
        config.sensorDelay);
    manager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
    if (manager.getSensorList(Sensor.TYPE_GYROSCOPE).isEmpty()) {
      gyroscopeAvailable = false;
    } else {
      Sensor gyroscope = manager.getSensorList(Sensor.TYPE_GYROSCOPE).get(0);
      gyroscopeListener = new SensorListener();
      gyroscopeAvailable = manager.registerListener(gyroscopeListener, gyroscope,
        config.sensorDelay);
    if (manager == null) manager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
    List<Sensor> rotationVectorSensors = manager.getSensorList(Sensor.TYPE_ROTATION_VECTOR);
    if (!rotationVectorSensors.isEmpty()){
      rotationVectorListener = new SensorListener();
      for (Sensor sensor : rotationVectorSensors){ // favor AOSP sensor
        if (sensor.getVendor().equals("Google Inc.") && sensor.getVersion() == 3){
          rotationVectorAvailable = manager.registerListener(rotationVectorListener, sensor,
            config.sensorDelay);
          break;

代码示例来源:origin: libgdx/libgdx

private void updateOrientation () {
  if (rotationVectorAvailable){
    SensorManager.getRotationMatrixFromVector(R, rotationVectorValues);
  } else if (!SensorManager.getRotationMatrix(R, null, accelerometerValues, magneticFieldValues)) {
      return; // compass + accelerometer in free fall
  }
  SensorManager.getOrientation(R, orientation);
  azimuth = (float)Math.toDegrees(orientation[0]);
  pitch = (float)Math.toDegrees(orientation[1]);
  roll = (float)Math.toDegrees(orientation[2]);
}

代码示例来源:origin: lipangit/JiaoZiVideoPlayer

@Override
protected void onResume() {
  super.onResume();
  Sensor accelerometerSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
  sensorManager.registerListener(sensorEventListener, accelerometerSensor, SensorManager.SENSOR_DELAY_NORMAL);
}

代码示例来源:origin: google/ExoPlayer

public SphericalSurfaceView(Context context, @Nullable AttributeSet attributeSet) {
 super(context, attributeSet);
 mainHandler = new Handler(Looper.getMainLooper());
 // Configure sensors and touch.
 sensorManager =
   (SensorManager) Assertions.checkNotNull(context.getSystemService(Context.SENSOR_SERVICE));
 Sensor orientationSensor = null;
 if (Util.SDK_INT >= 18) {
  // TYPE_GAME_ROTATION_VECTOR is the easiest sensor since it handles all the complex math for
  // fusion. It's used instead of TYPE_ROTATION_VECTOR since the latter uses the magnetometer on
  // devices. When used indoors, the magnetometer can take some time to settle depending on the
  // device and amount of metal in the environment.
  orientationSensor = sensorManager.getDefaultSensor(Sensor.TYPE_GAME_ROTATION_VECTOR);
 }
 if (orientationSensor == null) {
  orientationSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
 }
 this.orientationSensor = orientationSensor;
 scene = new SceneRenderer();
 renderer = new Renderer(scene);
 touchTracker = new TouchTracker(context, renderer, PX_PER_DEGREES);
 WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
 Display display = Assertions.checkNotNull(windowManager).getDefaultDisplay();
 phoneOrientationListener = new PhoneOrientationListener(display, touchTracker, renderer);
 setEGLContextClientVersion(2);
 setRenderer(renderer);
 setOnTouchListener(touchTracker);
}

代码示例来源:origin: guardianproject/haven

public BarometerMonitor(Context context) {
  prefs = new PreferenceManager(context);
  context.bindService(new Intent(context,
      MonitorService.class), mConnection, Context.BIND_ABOVE_CLIENT);
  sensorMgr = (SensorManager) context.getSystemService(AppCompatActivity.SENSOR_SERVICE);
  sensor = sensorMgr.getDefaultSensor(Sensor.TYPE_PRESSURE);
  if (sensor == null) {
    Log.i("Pressure", "Warning: no barometer sensor");
  } else {
    sensorMgr.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
  }
}

代码示例来源:origin: guardianproject/haven

public BumpMonitor(Context context) {
  context.bindService(new Intent(context,
      MonitorService.class), mConnection, Context.BIND_ABOVE_CLIENT);
  sensorMgr = (SensorManager) context.getSystemService(AppCompatActivity.SENSOR_SERVICE);
  bumpSensor = sensorMgr.getDefaultSensor(Sensor.TYPE_SIGNIFICANT_MOTION);
  if (bumpSensor == null) {
    Log.i("BumpMonitor", "Warning: no significant motion sensor");
  } else {
    boolean registered = sensorMgr.requestTriggerSensor(sensorListener, bumpSensor);
    Log.i("BumpMonitor", "Significant motion sensor registered: "+registered);
  }
}

代码示例来源:origin: safetysystemtechnology/android-shake-detector

public ShakeDetector startService(Context context) {
  this.shakeListener = new ShakeListener(this.shakeOptions, context);
  this.sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
  List<Sensor> sensors = this.sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER);
  if (sensors.size() > 0) {
    sensor = sensors.get(0);
    isRunning = this.sensorManager.registerListener(this.shakeListener, sensor, SensorManager.SENSOR_DELAY_GAME);
  }
  return this;
}

代码示例来源:origin: nisrulz/sensey

private void stopSensorDetection(SensorDetector detector) {
    if (detector != null && sensorManager != null) {
      sensorManager.unregisterListener(detector);
    }
  }
}

代码示例来源:origin: stackoverflow.com

/* put this into your activity class */
private SensorManager mSensorManager;
private float mAccel; // acceleration apart from gravity
private float mAccelCurrent; // current acceleration including gravity
private float mAccelLast; // last acceleration including gravity
private final SensorEventListener mSensorListener = new SensorEventListener() {
 public void onSensorChanged(SensorEvent se) {
  float x = se.values[0];
  float y = se.values[1];
  float z = se.values[2];
  mAccelLast = mAccelCurrent;
  mAccelCurrent = (float) Math.sqrt((double) (x*x + y*y + z*z));
  float delta = mAccelCurrent - mAccelLast;
  mAccel = mAccel * 0.9f + delta; // perform low-cut filter
 }
 public void onAccuracyChanged(Sensor sensor, int accuracy) {
 }
};
@Override
protected void onResume() {
 super.onResume();
 mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
 mSensorManager.unregisterListener(mSensorListener);
 super.onPause();
}

代码示例来源:origin: novoda/android-demos

private void registerAcclSensor() {
  if (sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER).size() > 0) {
    acclSensor = sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER).get(FIRST_SENSOR_ITEM);
    sensorManager.registerListener(this, acclSensor, SensorManager.SENSOR_DELAY_NORMAL);
  }
};

代码示例来源:origin: guardianproject/haven

public void stop(Context context) {
  sensorMgr.unregisterListener(this);
  context.unbindService(mConnection);
}

代码示例来源:origin: nisrulz/sensey

private Iterable<Sensor> convertTypesToSensors(int... sensorTypes) {
  Collection<Sensor> sensors = new ArrayList<>();
  if (sensorManager != null) {
    for (int sensorType : sensorTypes) {
      sensors.add(sensorManager.getDefaultSensor(sensorType));
    }
  }
  return sensors;
}

代码示例来源:origin: nisrulz/sensey

private void registerDetectorForAllSensors(SensorDetector detector, Iterable<Sensor> sensors) {
  for (Sensor sensor : sensors) {
    sensorManager.registerListener(detector, sensor, samplingPeriod);
  }
}

代码示例来源:origin: commonsguy/cw-omnibus

@Override
public void onSensorSelected(Sensor s) {
 mgr.unregisterListener(log);
 mgr.registerListener(log, s, SensorManager.SENSOR_DELAY_NORMAL);
 log.init(isXYZ(s));
 panes.closePane();
}

代码示例来源:origin: robolectric/robolectric

@Test
public void shouldReturnHasNoListenerAfterUnregisterListener() {
 SensorEventListener listener = registerListener();
 sensorManager.unregisterListener(listener, sensorManager.getDefaultSensor(SensorManager.SENSOR_ACCELEROMETER));
 assertThat(shadow.hasListener(listener)).isFalse();
}

代码示例来源:origin: doggycoder/AndroidOpenGLDemo

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.glview);
  mSensorManager=(SensorManager)getSystemService(Context.SENSOR_SERVICE);
  List<Sensor> sensors=mSensorManager.getSensorList(Sensor.TYPE_ALL);
  //todo 判断是否存在rotation vector sensor
  mRotation=mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
  mGLView=(GLSurfaceView) findViewById(R.id.mGLView);
  mGLView.setEGLContextClientVersion(2);
  mGLView.setRenderer(this);
  mGLView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
  mSkySphere=new SkySphere(this.getApplicationContext(),"vr/360sp.jpg");
}

代码示例来源:origin: aporter/coursera-android

boolean success = SensorManager.getRotationMatrix(rotationMatrix,
    null, mGravity, mGeomagnetic);
  SensorManager.getOrientation(rotationMatrix, orientationMatrix);

代码示例来源:origin: nisrulz/sensey

@Override
  protected void onSensorEvent(SensorEvent sensorEvent) {
    // Get rotation matrix
    float[] rotationMatrix = new float[16];
    SensorManager.getRotationMatrixFromVector(rotationMatrix, sensorEvent.values);

    // Remap coordinate system
    float[] remappedRotationMatrix = new float[16];
    SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_X, SensorManager.AXIS_Z,
        remappedRotationMatrix);

    // Convert to orientations
    float[] orientations = new float[3];
    SensorManager.getOrientation(remappedRotationMatrix, orientations);

    // Convert values in radian to degrees
    for (int i = 0; i < 3; i++) {
      orientations[i] = (float) (Math.toDegrees(orientations[i]));
    }

    rotationAngleListener.onRotation(orientations[0], orientations[1], orientations[2]);
  }
}

相关文章