android.hardware.SensorManager.getDefaultSensor()方法的使用及代码示例

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

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

SensorManager.getDefaultSensor介绍

暂无

代码示例

代码示例来源: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: 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: gjiazhe/PanoramaImageView

public void register(Context context) {
  if (mSensorManager == null) {
    mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
  }
  Sensor mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
  mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_FASTEST);
  mLastTimestamp = 0;
  mRotateRadianY = mRotateRadianX = 0;
}

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

public class yourActivity extends Activity implements SensorEventListener{
 private SensorManager sensorManager;
 double ax,ay,az;   // these are the acceleration in x,y and z axis
 @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    sensorManager=(SensorManager) getSystemService(SENSOR_SERVICE);
    sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
  }
  @Override
  public void onAccuracyChanged(Sensor arg0, int arg1) {
  }

  @Override
  public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
      ax=event.values[0];
          ay=event.values[1];
          az=event.values[2];
      }
  }
}

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

@Override
protected void onResume() {
  super.onResume();
  Sensor accelerometerSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
  mSensorManager.registerListener(mSensorEventListener, accelerometerSensor, SensorManager.SENSOR_DELAY_NORMAL);
  //home back
  Jzvd.goOnPlayOnResume();
}

代码示例来源:origin: square/seismic

/**
 * Starts listening for shakes on devices with appropriate hardware.
 *
 * @return true if the device supports shake detection.
 */
public boolean start(SensorManager sensorManager) {
 // Already started?
 if (accelerometer != null) {
  return true;
 }
 accelerometer = sensorManager.getDefaultSensor(
   Sensor.TYPE_ACCELEROMETER);
 // If this phone has an accelerometer, listen to it.
 if (accelerometer != null) {
  this.sensorManager = sensorManager;
  sensorManager.registerListener(this, accelerometer,
    SensorManager.SENSOR_DELAY_FASTEST);
 }
 return accelerometer != null;
}

代码示例来源: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: journeyapps/zxing-android-embedded

public void start() {
  if (cameraSettings.isAutoTorchEnabled()) {
    SensorManager sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
    lightSensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
    if (lightSensor != null) {
      sensorManager.registerListener(this, lightSensor, SensorManager.SENSOR_DELAY_NORMAL);
    }
  }
}

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

private void startAccel () {
    try {
      SensorManager sensorMgr = (SensorManager) getSystemService(AppCompatActivity.SENSOR_SERVICE);
      Sensor sensor = sensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
      if (sensor == null) {
        Log.i("AccelerometerFrament", "Warning: no accelerometer");
      } else {
        sensorMgr.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
      }
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
}

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

SensorManager mSensorMgr = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE);

HandlerThread mHandlerThread = new HandlerThread("sensorThread");

mHandlerThread.start();

Handler handler = new Handler(mHandlerThread.getLooper());

mSensorMgr.registerListener(this, mSensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
          SensorManager.SENSOR_DELAY_FASTEST, handler);

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

mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
  mShakeDetector = new ShakeDetector(new OnShakeListener() {
    @Override
protected void onResume() {
  super.onResume();
  mSensorManager.registerListener(mShakeDetector, mAccelerometer, SensorManager.SENSOR_DELAY_UI);

代码示例来源:origin: jokermonn/permissions4m

/**
 * use sensors, {@link android.Manifest.permission#BODY_SENSORS}
 *
 * @param activity
 * @return true if success
 * @throws Exception
 */
private static boolean checkBodySensors(Activity activity) throws Exception {
  SensorManager sensorManager = (SensorManager) activity.getSystemService(SENSOR_SERVICE);
  Sensor sensor = sensorManager.getDefaultSensor((Sensor.TYPE_ACCELEROMETER));
  SensorEventListener listener = new SensorEventListener() {
    @Override
    public void onSensorChanged(SensorEvent event) {
    }
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }
  };
  sensorManager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_GAME);
  sensorManager.unregisterListener(listener, sensor);
  return true;
}

代码示例来源: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 AmbientLightMonitor(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);
  //noinspection RedundantCast
  sensor = (Sensor) sensorMgr.getDefaultSensor(Sensor.TYPE_LIGHT);
  if (sensor == null) {
    Log.i("AccelerometerFrament", "Warning: no accelerometer");
  } else {
    sensorMgr.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
  }
}

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

super.onResume();
Sensor gsensor = sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);
Sensor asensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
Sensor msensor = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
sensorManager.registerListener(this, gsensor, SensorManager.SENSOR_DELAY_GAME);
sensorManager.registerListener(this, asensor, SensorManager.SENSOR_DELAY_GAME);
sensorManager.registerListener(this, msensor, SensorManager.SENSOR_DELAY_GAME);

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

mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mMagnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
mLastAccelerometerSet = false;
mLastMagnetometerSet = false;
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
mSensorManager.registerListener(this, mMagnetometer, SensorManager.SENSOR_DELAY_NORMAL);

代码示例来源:origin: florent37/CameraFragment

@Override
public void onResume() {
  super.onResume();
  cameraController.onResume();
  sensorManager.registerListener(sensorEventListener, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
  if (cameraFragmentControlsListener != null) {
    cameraFragmentControlsListener.lockControls();
    cameraFragmentControlsListener.allowRecord(false);
  }
}

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

public AccelerometerMonitor(Context context) {
  prefs = new PreferenceManager(context);
  /*
   * Set sensitivity value
   */
try {
    shakeThreshold = Integer.parseInt(prefs.getAccelerometerSensitivity());
  }
  catch (Exception e)
  {
    shakeThreshold = 50;
  }
  context.bindService(new Intent(context,
      MonitorService.class), mConnection, Context.BIND_ABOVE_CLIENT);
  sensorMgr = (SensorManager) context.getSystemService(AppCompatActivity.SENSOR_SERVICE);
  accelerometer = sensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
  if (accelerometer == null) {
    Log.i("AccelerometerFrament", "Warning: no accelerometer");
  } else {
    sensorMgr.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
  }
}

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

private SensorEventListener registerListener() {
 SensorEventListener listener = new TestSensorEventListener();
 Sensor sensor = sensorManager.getDefaultSensor(SensorManager.SENSOR_ACCELEROMETER);
 sensorManager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_NORMAL);
 return listener;
}

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

Sensor accelerometer = manager.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
  accelerometerListener = new SensorListener();
  accelerometerAvailable = manager.registerListener(accelerometerListener, accelerometer,
    config.sensorDelay);
  Sensor gyroscope = manager.getSensorList(Sensor.TYPE_GYROSCOPE).get(0);
  gyroscopeListener = new SensorListener();
  gyroscopeAvailable = manager.registerListener(gyroscopeListener, gyroscope,
    config.sensorDelay);
  for (Sensor sensor : rotationVectorSensors){ // favor AOSP sensor
    if (sensor.getVendor().equals("Google Inc.") && sensor.getVersion() == 3){
      rotationVectorAvailable = manager.registerListener(rotationVectorListener, sensor,
        config.sensorDelay);
      break;
Sensor sensor = manager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
if (sensor != null) {
  compassAvailable = accelerometerAvailable;

相关文章