当前位置:   article > 正文

Android Sensor (1) -- Sensor服务获取_sensorservice获取sensor type

sensorservice获取sensor type

 

目录

1.1 app调用sensor

1.4 createService and CachedServiceFetcher

1.5 SystemSensorManager init

1.6 app getDefaultSensor

1.7 app registerListener

1.8 SensorEventListener 数据回调 onSensorChanged


 

 

1.1 app调用sensor

//getSystemService --> getDefaultSensor --> registerListener

  1. //getSystemService -- getDefaultSensor -- registerListener
  2. //第一步:通过getSystemService获得SensorManager实例对象
  3. mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
  4. //第二步:通过SensorManager实例对象获得想要的传感器对象:参数决定获取哪个传感器
  5. mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
  6. mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);

 

1.2 getSystemService流程

  1. @frameworks/base/core/java/android/app/ContextImpl.java
  2. @Override
  3. public Object getSystemService(String name) {
  4. return SystemServiceRegistry.getSystemService(this, name);
  5. }
  6. //得到的是fetcher.getService(ctx)
  7. /**
  8. * Gets a system service from a given context.
  9. */
  10. public static Object getSystemService(ContextImpl ctx, String name) {
  11. ServiceFetcher<?> fetcher = SYSTEM_SERVICE_FETCHERS.get(name);
  12. return fetcher != null ? fetcher.getService(ctx) : null;
  13. }

1.3 SystemServiceRegistry register Sensor Service流程

//保存到SYSTEM_SERVICE_NAMES SYSTEM_SERVICE_FETCHERS,然后重写 createService

  1. @/frameworks/base/core/java/android/app/SystemServiceRegistry.java
  2. registerService(Context.SENSOR_SERVICE, SensorManager.class,
  3. new CachedServiceFetcher<SensorManager>() {
  4. @Override
  5. public SensorManager createService(ContextImpl ctx) {
  6. return new SystemSensorManager(ctx.getOuterContext(),
  7. ctx.mMainThread.getHandler().getLooper());
  8. }});
  9. /**
  10. * Statically registers a system service with the context.
  11. * This method must be called during static initialization only.
  12. */
  13. private static <T> void registerService(String serviceName, Class<T> serviceClass,
  14. ServiceFetcher<T> serviceFetcher) {
  15. SYSTEM_SERVICE_NAMES.put(serviceClass, serviceName);
  16. SYSTEM_SERVICE_FETCHERS.put(serviceName, serviceFetcher);
  17. }

1.4 createService and CachedServiceFetcher

//CachedServiceFetcher如下,fetcher.getService(ctx)获取到的返回参数就是 service = createService(ctx);也就是createService里面内容

  1. @frameworks/base/core/java/android/app/SystemServiceRegistry.java
  2. static abstract interface ServiceFetcher<T> {
  3. T getService(ContextImpl ctx);
  4. }
  5. /**
  6. * Override this class when the system service constructor needs a
  7. * ContextImpl and should be cached and retained by that context.
  8. */
  9. static abstract class CachedServiceFetcher<T> implements ServiceFetcher<T> {
  10. private final int mCacheIndex;
  11. public CachedServiceFetcher() {
  12. mCacheIndex = sServiceCacheSize++;
  13. }
  14. @Override
  15. @SuppressWarnings("unchecked")
  16. public final T getService(ContextImpl ctx) {
  17. final Object[] cache = ctx.mServiceCache;
  18. synchronized (cache) {
  19. // Fetch or create the service.
  20. Object service = cache[mCacheIndex];
  21. if (service == null) {
  22. try {
  23. service = createService(ctx);
  24. cache[mCacheIndex] = service;
  25. } catch (ServiceNotFoundException e) {
  26. onServiceNotFound(e);
  27. }
  28. }
  29. return (T)service;
  30. }
  31. }
  32. public abstract T createService(ContextImpl ctx) throws ServiceNotFoundException;
  33. }

1.5 SystemSensorManager init

//getSystemService 获取到的对象就是SystemSensorManager

  1. @frameworks/base/core/java/android/hardware/SystemSensorManager.java
  2. public class SystemSensorManager extends SensorManager {
  3. /** {@hide} */
  4. public SystemSensorManager(Context context, Looper mainLooper) {
  5. synchronized(sLock) {
  6. if (!sNativeClassInited) {
  7. sNativeClassInited = true;
  8. nativeClassInit();
  9. }
  10. }
  11. mMainLooper = mainLooper;
  12. mTargetSdkLevel = context.getApplicationInfo().targetSdkVersion;
  13. mContext = context;
  14. mNativeInstance = nativeCreate(context.getOpPackageName());
  15. // initialize the sensor list
  16. for (int index = 0;;++index) {
  17. Sensor sensor = new Sensor();
  18. if (!nativeGetSensorAtIndex(mNativeInstance, sensor, index)) break;
  19. mFullSensorsList.add(sensor);
  20. mHandleToSensor.put(sensor.getHandle(), sensor);
  21. }
  22. }
  23. }

1.6 app getDefaultSensor

  1. @frameworks/base/core/java/android/hardware/SensorManager.java
  2. public Sensor getDefaultSensor(int type) {
  3. // TODO: need to be smarter, for now, just return the 1st sensor
  4. List<Sensor> l = getSensorList(type);
  5. boolean wakeUpSensor = false;
  6. // For the following sensor types, return a wake-up sensor. These types are by default
  7. // defined as wake-up sensors. For the rest of the SDK defined sensor types return a
  8. // non_wake-up version.
  9. if (type == Sensor.TYPE_PROXIMITY || type == Sensor.TYPE_SIGNIFICANT_MOTION ||
  10. type == Sensor.TYPE_TILT_DETECTOR || type == Sensor.TYPE_WAKE_GESTURE ||
  11. type == Sensor.TYPE_GLANCE_GESTURE || type == Sensor.TYPE_PICK_UP_GESTURE ||
  12. type == Sensor.TYPE_WRIST_TILT_GESTURE || type == Sensor.TYPE_DYNAMIC_SENSOR_META) {
  13. wakeUpSensor = true;
  14. }
  15. for (Sensor sensor : l) {
  16. if (sensor.isWakeUpSensor() == wakeUpSensor) return sensor;
  17. }
  18. return null;
  19. }

1.7 app registerListener

  1. @frameworks/base/core/java/android/hardware/SensorManager.java
  2. //SensorEventListener
  3. public boolean registerListener(SensorEventListener listener, Sensor sensor,
  4. int samplingPeriodUs) {
  5. return registerListener(listener, sensor, samplingPeriodUs, null);
  6. }
  7. public boolean registerListener(SensorEventListener listener, Sensor sensor,
  8. int samplingPeriodUs, int maxReportLatencyUs) {
  9. int delay = getDelay(samplingPeriodUs);
  10. return registerListenerImpl(listener, sensor, delay, null, maxReportLatencyUs, 0);
  11. }
  12. /** @hide */
  13. protected abstract boolean registerListenerImpl(SensorEventListener listener, Sensor sensor,
  14. int delayUs, Handler handler, int maxReportLatencyUs, int reservedFlags);
  15. @frameworks/base/core/java/android/hardware/SystemSensorManager.java
  16. /** @hide */
  17. @Override
  18. protected boolean registerListenerImpl(SensorEventListener listener, Sensor sensor,
  19. int delayUs, Handler handler, int maxBatchReportLatencyUs, int reservedFlags) {
  20. ...
  21. // We map SensorEventListener to a SensorEventQueue, which holds the looper
  22. synchronized (mSensorListeners) {
  23. SensorEventQueue queue = mSensorListeners.get(listener);
  24. if (queue == null) {
  25. Looper looper = (handler != null) ? handler.getLooper() : mMainLooper;
  26. final String fullClassName = listener.getClass().getEnclosingClass() != null ?
  27. listener.getClass().getEnclosingClass().getName() :
  28. listener.getClass().getName();
  29. queue = new SensorEventQueue(listener, looper, this, fullClassName);
  30. if (!queue.addSensor(sensor, delayUs, maxBatchReportLatencyUs)) {
  31. queue.dispose();
  32. return false;
  33. }
  34. mSensorListeners.put(listener, queue);
  35. return true;
  36. } else {
  37. return queue.addSensor(sensor, delayUs, maxBatchReportLatencyUs);
  38. }
  39. }
  40. }

1.8 SensorEventListener 数据回调 onSensorChanged

  1. @Override
  2. public final void onSensorChanged(SensorEvent event) {
  3. // 大部分传感器会返回三个轴方向x,y,x的event值,值的意义因传感器而异
  4. float x = event.values[0];
  5. float y = event.values[1];
  6. float z = event.values[2];
  7. //TODO 利用获得的三个float传感器值做些操作
  8. }

 

参考Link:

https://blog.csdn.net/pengtgimust/article/details/51227289

http://www.360doc.com/content/19/0128/11/15700426_811745365.shtml

https://www.jianshu.com/p/8440a5897944

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/778966
推荐阅读
相关标签
  

闽ICP备14008679号