赞
踩
目录
1.4 createService and CachedServiceFetcher
1.8 SensorEventListener 数据回调 onSensorChanged
//getSystemService --> getDefaultSensor --> registerListener
- //getSystemService -- getDefaultSensor -- registerListener
- //第一步:通过getSystemService获得SensorManager实例对象
- mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
- //第二步:通过SensorManager实例对象获得想要的传感器对象:参数决定获取哪个传感器
- mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
- mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);
1.2 getSystemService流程
- @frameworks/base/core/java/android/app/ContextImpl.java
- @Override
- public Object getSystemService(String name) {
- return SystemServiceRegistry.getSystemService(this, name);
- }
-
- //得到的是fetcher.getService(ctx)
- /**
- * Gets a system service from a given context.
- */
- public static Object getSystemService(ContextImpl ctx, String name) {
- ServiceFetcher<?> fetcher = SYSTEM_SERVICE_FETCHERS.get(name);
- return fetcher != null ? fetcher.getService(ctx) : null;
- }
1.3 SystemServiceRegistry register Sensor Service流程
//保存到SYSTEM_SERVICE_NAMES SYSTEM_SERVICE_FETCHERS,然后重写 createService
- @/frameworks/base/core/java/android/app/SystemServiceRegistry.java
- registerService(Context.SENSOR_SERVICE, SensorManager.class,
- new CachedServiceFetcher<SensorManager>() {
- @Override
- public SensorManager createService(ContextImpl ctx) {
- return new SystemSensorManager(ctx.getOuterContext(),
- ctx.mMainThread.getHandler().getLooper());
- }});
-
- /**
- * Statically registers a system service with the context.
- * This method must be called during static initialization only.
- */
- private static <T> void registerService(String serviceName, Class<T> serviceClass,
- ServiceFetcher<T> serviceFetcher) {
- SYSTEM_SERVICE_NAMES.put(serviceClass, serviceName);
- SYSTEM_SERVICE_FETCHERS.put(serviceName, serviceFetcher);
- }
//CachedServiceFetcher如下,fetcher.getService(ctx)获取到的返回参数就是 service = createService(ctx);也就是createService里面内容
- @frameworks/base/core/java/android/app/SystemServiceRegistry.java
- static abstract interface ServiceFetcher<T> {
- T getService(ContextImpl ctx);
- }
-
- /**
- * Override this class when the system service constructor needs a
- * ContextImpl and should be cached and retained by that context.
- */
- static abstract class CachedServiceFetcher<T> implements ServiceFetcher<T> {
- private final int mCacheIndex;
-
- public CachedServiceFetcher() {
- mCacheIndex = sServiceCacheSize++;
- }
-
- @Override
- @SuppressWarnings("unchecked")
- public final T getService(ContextImpl ctx) {
- final Object[] cache = ctx.mServiceCache;
- synchronized (cache) {
- // Fetch or create the service.
- Object service = cache[mCacheIndex];
- if (service == null) {
- try {
- service = createService(ctx);
- cache[mCacheIndex] = service;
- } catch (ServiceNotFoundException e) {
- onServiceNotFound(e);
- }
- }
- return (T)service;
- }
- }
-
- public abstract T createService(ContextImpl ctx) throws ServiceNotFoundException;
- }
//getSystemService 获取到的对象就是SystemSensorManager
- @frameworks/base/core/java/android/hardware/SystemSensorManager.java
- public class SystemSensorManager extends SensorManager {
- /** {@hide} */
- public SystemSensorManager(Context context, Looper mainLooper) {
- synchronized(sLock) {
- if (!sNativeClassInited) {
- sNativeClassInited = true;
- nativeClassInit();
- }
- }
-
- mMainLooper = mainLooper;
- mTargetSdkLevel = context.getApplicationInfo().targetSdkVersion;
- mContext = context;
- mNativeInstance = nativeCreate(context.getOpPackageName());
-
- // initialize the sensor list
- for (int index = 0;;++index) {
- Sensor sensor = new Sensor();
- if (!nativeGetSensorAtIndex(mNativeInstance, sensor, index)) break;
- mFullSensorsList.add(sensor);
- mHandleToSensor.put(sensor.getHandle(), sensor);
- }
- }
- }
- @frameworks/base/core/java/android/hardware/SensorManager.java
- public Sensor getDefaultSensor(int type) {
- // TODO: need to be smarter, for now, just return the 1st sensor
- List<Sensor> l = getSensorList(type);
- boolean wakeUpSensor = false;
- // For the following sensor types, return a wake-up sensor. These types are by default
- // defined as wake-up sensors. For the rest of the SDK defined sensor types return a
- // non_wake-up version.
- if (type == Sensor.TYPE_PROXIMITY || type == Sensor.TYPE_SIGNIFICANT_MOTION ||
- type == Sensor.TYPE_TILT_DETECTOR || type == Sensor.TYPE_WAKE_GESTURE ||
- type == Sensor.TYPE_GLANCE_GESTURE || type == Sensor.TYPE_PICK_UP_GESTURE ||
- type == Sensor.TYPE_WRIST_TILT_GESTURE || type == Sensor.TYPE_DYNAMIC_SENSOR_META) {
- wakeUpSensor = true;
- }
-
- for (Sensor sensor : l) {
- if (sensor.isWakeUpSensor() == wakeUpSensor) return sensor;
- }
- return null;
- }
- @frameworks/base/core/java/android/hardware/SensorManager.java
- //SensorEventListener
- public boolean registerListener(SensorEventListener listener, Sensor sensor,
- int samplingPeriodUs) {
- return registerListener(listener, sensor, samplingPeriodUs, null);
- }
-
- public boolean registerListener(SensorEventListener listener, Sensor sensor,
- int samplingPeriodUs, int maxReportLatencyUs) {
- int delay = getDelay(samplingPeriodUs);
- return registerListenerImpl(listener, sensor, delay, null, maxReportLatencyUs, 0);
- }
- /** @hide */
- protected abstract boolean registerListenerImpl(SensorEventListener listener, Sensor sensor,
- int delayUs, Handler handler, int maxReportLatencyUs, int reservedFlags);
-
- @frameworks/base/core/java/android/hardware/SystemSensorManager.java
- /** @hide */
- @Override
- protected boolean registerListenerImpl(SensorEventListener listener, Sensor sensor,
- int delayUs, Handler handler, int maxBatchReportLatencyUs, int reservedFlags) {
- ...
-
- // We map SensorEventListener to a SensorEventQueue, which holds the looper
- synchronized (mSensorListeners) {
- SensorEventQueue queue = mSensorListeners.get(listener);
- if (queue == null) {
- Looper looper = (handler != null) ? handler.getLooper() : mMainLooper;
- final String fullClassName = listener.getClass().getEnclosingClass() != null ?
- listener.getClass().getEnclosingClass().getName() :
- listener.getClass().getName();
- queue = new SensorEventQueue(listener, looper, this, fullClassName);
- if (!queue.addSensor(sensor, delayUs, maxBatchReportLatencyUs)) {
- queue.dispose();
- return false;
- }
- mSensorListeners.put(listener, queue);
- return true;
- } else {
- return queue.addSensor(sensor, delayUs, maxBatchReportLatencyUs);
- }
- }
- }
- @Override
- public final void onSensorChanged(SensorEvent event) {
- // 大部分传感器会返回三个轴方向x,y,x的event值,值的意义因传感器而异
- float x = event.values[0];
- float y = event.values[1];
- float z = event.values[2];
- //TODO 利用获得的三个float传感器值做些操作
- }
参考Link:
https://blog.csdn.net/pengtgimust/article/details/51227289
http://www.360doc.com/content/19/0128/11/15700426_811745365.shtml
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。