Android传感器API之:加速度Accelerometer功能源码

加速度传感器,主要是感应手机的运动。捕获三个参数,分别表示空间坐标系中X、Y、Z轴方向上的加速度减去重力加速度在相应轴上的分量,其单位均为m/s2。和之前几篇传感器功能介绍中的一样,这几个方向就不再多说了~~

Rexsee的源码如下,有兴趣的朋友可以再去找找,社区公开的源码里有包括加速度在内的,重力、磁场、温度、距离和方向等传感器功能。 http://www.rexsee.com/CN/helpReference.php

  1. /*
  2. * Copyright (C) 2011 The Rexsee Open Source Project
  3. *
  4. * Licensed under the Rexsee License, Version 1.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. *      http://www.rexsee.com/CN/legal/license.html
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */

  16. package rexsee.sensor; 

  17. import rexsee.core.browser.JavascriptInterface; 
  18. import rexsee.core.browser.RexseeBrowser; 
  19. import android.content.Context; 
  20. import android.hardware.Sensor; 
  21. import android.hardware.SensorEvent; 
  22. import android.hardware.SensorEventListener; 
  23. import android.hardware.SensorManager; 

  24. public class RexseeSensorAccelerometer implements JavascriptInterface { 

  25.        private static final String INTERFACE_NAME = "Accelerometer"; 
  26.        @Override 
  27.        public String getInterfaceName() { 
  28.                return mBrowser.application.resources.prefix + INTERFACE_NAME; 
  29.        } 
  30.        @Override 
  31.        public JavascriptInterface getInheritInterface(RexseeBrowser childBrowser) { 
  32.                return this; 
  33.        } 
  34.        @Override 
  35.        public JavascriptInterface getNewInterface(RexseeBrowser childBrowser) { 
  36.                return new RexseeSensorAccelerometer(childBrowser); 
  37.        } 

  38.        public static final String EVENT_ONACCELERATE = "onAccelerometerChanged"; 

  39.        private final Context mContext; 
  40.        private final RexseeBrowser mBrowser; 
  41.        private final SensorManager mSensorManager; 
  42.        private final SensorEventListener mSensorListener; 
  43.        private final Sensor mSensor; 

  44.        private int mRate = SensorManager.SENSOR_DELAY_NORMAL; 
  45.        private int mCycle = 100; 
  46.        private int mEventCycle = 100; 

  47.        private float mAccuracyX = 0; 
  48.        private float mAccuracyY = 0; 
  49.        private float mAccuracyZ = 0; 

  50.        private long lastUpdate = -1; 
  51.        private long lastEvent = -1; 

  52.        private float x = -999, y = -999, z = -999; 

  53.        public RexseeSensorAccelerometer(RexseeBrowser browser) { 

  54.                mContext = browser.getContext(); 
  55.                mBrowser = browser; 
  56.                browser.eventList.add(EVENT_ONACCELERATE); 

  57.                mSensorManager = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE); 

  58.                mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 

  59.                mSensorListener = new SensorEventListener() { 
  60.                        @Override 
  61.                        public void onAccuracyChanged(Sensor sensor, int accuracy) { 
  62.                        } 
  63.                        @Override 
  64.                        public void onSensorChanged(SensorEvent event) { 
  65.                                if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER) return; 
  66.                                long curTime = System.currentTimeMillis(); 
  67.                                if (lastUpdate == -1 || (curTime - lastUpdate) > mCycle) { 
  68.                                        lastUpdate = curTime; 
  69.                                        float lastX = x; 
  70.                                        float lastY = y; 
  71.                                        float lastZ = z; 
  72.                                        x = event.values[SensorManager.DATA_X]; 
  73.                                        y = event.values[SensorManager.DATA_Y]; 
  74.                                        z = event.values[SensorManager.DATA_Z]; 
  75.                                        if (lastEvent == -1 || (curTime - lastEvent) > mEventCycle) { 
  76.                                                if ( 
  77.                                                                (mAccuracyX >= 0 && Math.abs(x - lastX) > mAccuracyX) 
  78.                                                                                || (mAccuracyY >= 0 && Math.abs(y - lastY) > mAccuracyY) 
  79.                                                                                || (mAccuracyZ >= 0 && Math.abs(z - lastZ) > mAccuracyZ) 
  80.                                                        ) { 
  81.                                                                lastEvent = curTime; 
  82.                                                                mBrowser.eventList.run(EVENT_ONACCELERATE); 
  83.                                                        } 
  84.                                                } 
  85.                                        } 
  86.                                } 
  87.                }; 

  88.        } 

  89.        public String getLastKnownX() { 
  90.                return (x == -999) ? "null" : String.valueOf(x); 
  91.        } 
  92.        public String getLastKnownY() { 
  93.                return (y == -999) ? "null" : String.valueOf(y); 
  94.        } 
  95.        public String getLastKnownZ() { 
  96.                return (z == -999) ? "null" : String.valueOf(z); 
  97.        } 

  98.        public void setRate(String rate) { 
  99.                mRate = SensorRate.getInt(rate); 
  100.        } 
  101.        public String getRate() { 
  102.                return SensorRate.getString(mRate); 
  103.        } 
  104.        public void setCycle(int milliseconds) { 
  105.                mCycle = milliseconds; 
  106.        } 
  107.        public int getCycle() { 
  108.                return mCycle; 
  109.        } 
  110.        public void setEventCycle(int milliseconds) { 
  111.                mEventCycle = milliseconds; 
  112.        } 
  113.        public int getEventCycle() { 
  114.                return mEventCycle; 
  115.        } 
  116.        public void setAccuracy(float x, float y, float z) { 
  117.                mAccuracyX = x; 
  118.                mAccuracyY = y; 
  119.                mAccuracyZ = z; 
  120.        } 
  121.        public float getAccuracyX() { 
  122.                return mAccuracyX; 
  123.        } 
  124.        public float getAccuracyY() { 
  125.                return mAccuracyY; 
  126.        } 
  127.        public float getAccuracyZ() { 
  128.                return mAccuracyZ; 
  129.        } 

  130.        public boolean isReady() { 
  131.                return (mSensor == null) ? false : true; 
  132.        } 
  133.        public void start() { 
  134.                if (isReady()) { 
  135.                        mSensorManager.registerListener(mSensorListener, mSensor, mRate); 
  136.                } else { 
  137.                        mBrowser.exception(getInterfaceName(), "Accelerometer is not found."); 
  138.                } 
  139.        } 
  140.        public void stop() { 
  141.                if (isReady()) { 
  142.                        mSensorManager.unregisterListener(mSensorListener); 
  143.                } 
  144.        } 

  145. }

你可能感兴趣的:(android)