| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1043 人关注过本帖
标题:Android传感器API之:加速度Accelerometer功能源码
只看楼主 加入收藏
hkjwz
Rank: 2
等 级:论坛游民
帖 子:6
专家分:20
注 册:2011-11-29
结帖率:0
收藏
已结贴  问题点数:20 回复次数:7 
Android传感器API之:加速度Accelerometer功能源码


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

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

/*
* Copyright (C) 2011 The Rexsee Open Source Project
*
* Licensed under the Rexsee License, Version 1.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
 
package rexsee.sensor;  
 
import rexsee.core.browser.JavascriptInterface;  
import rexsee.core.browser.RexseeBrowser;  
import android.content.Context;  
import android.hardware.Sensor;  
import android.hardware.SensorEvent;  
import android.hardware.SensorEventListener;  
import android.hardware.SensorManager;  
 
public class RexseeSensorAccelerometer implements JavascriptInterface {  
 
       private static final String INTERFACE_NAME = "Accelerometer";  
       @Override  
       public String getInterfaceName() {  
               return mBrowser.application.resources.prefix + INTERFACE_NAME;  
       }  
       @Override  
       public JavascriptInterface getInheritInterface(RexseeBrowser childBrowser) {  
               return this;  
       }  
       @Override  
       public JavascriptInterface getNewInterface(RexseeBrowser childBrowser) {  
               return new RexseeSensorAccelerometer(childBrowser);  
       }  
 
       public static final String EVENT_ONACCELERATE = "onAccelerometerChanged";  
 
       private final Context mContext;  
       private final RexseeBrowser mBrowser;  
       private final SensorManager mSensorManager;  
       private final SensorEventListener mSensorListener;  
       private final Sensor mSensor;  
 
       private int mRate = SensorManager.SENSOR_DELAY_NORMAL;  
       private int mCycle = 100;  
       private int mEventCycle = 100;  
 
       private float mAccuracyX = 0;  
       private float mAccuracyY = 0;  
       private float mAccuracyZ = 0;  
 
       private long lastUpdate = -1;  
       private long lastEvent = -1;  
 
       private float x = -999, y = -999, z = -999;  
 
       public RexseeSensorAccelerometer(RexseeBrowser browser) {  
 
               mContext = browser.getContext();  
               mBrowser = browser;  
               browser.eventList.add(EVENT_ONACCELERATE);  
 
               mSensorManager = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE);  
 
               mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);  
 
               mSensorListener = new SensorEventListener() {  
                       @Override  
                       public void onAccuracyChanged(Sensor sensor, int accuracy) {  
                       }  
                       @Override  
                       public void onSensorChanged(SensorEvent event) {  
                               if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER) return;  
                               long curTime = System.currentTimeMillis();  
                               if (lastUpdate == -1 || (curTime - lastUpdate) > mCycle) {  
                                       lastUpdate = curTime;  
                                       float lastX = x;  
                                       float lastY = y;  
                                       float lastZ = z;  
                                       x = event.values[SensorManager.DATA_X];  
                                       y = event.values[SensorManager.DATA_Y];  
                                       z = event.values[SensorManager.DATA_Z];  
                                       if (lastEvent == -1 || (curTime - lastEvent) > mEventCycle) {  
                                               if (  
                                                               (mAccuracyX >= 0 && Math.abs(x - lastX) > mAccuracyX)  
                                                                               || (mAccuracyY >= 0 && Math.abs(y - lastY) > mAccuracyY)  
                                                                               || (mAccuracyZ >= 0 && Math.abs(z - lastZ) > mAccuracyZ)  
                                                       ) {  
                                                               lastEvent = curTime;  
                                                               mBrowser.eventList.run(EVENT_ONACCELERATE);  
                                                       }  
                                               }  
                                       }  
                               }  
               };  
 
       }  
 
       public String getLastKnownX() {  
               return (x == -999) ? "null" : String.valueOf(x);  
       }  
       public String getLastKnownY() {  
               return (y == -999) ? "null" : String.valueOf(y);  
       }  
       public String getLastKnownZ() {  
               return (z == -999) ? "null" : String.valueOf(z);  
       }  
 
       public void setRate(String rate) {  
               mRate = SensorRate.getInt(rate);  
       }  
       public String getRate() {  
               return SensorRate.getString(mRate);  
       }  
       public void setCycle(int milliseconds) {  
               mCycle = milliseconds;  
       }  
       public int getCycle() {  
               return mCycle;  
       }  
       public void setEventCycle(int milliseconds) {  
               mEventCycle = milliseconds;  
       }  
       public int getEventCycle() {  
               return mEventCycle;  
       }  
       public void setAccuracy(float x, float y, float z) {  
               mAccuracyX = x;  
               mAccuracyY = y;  
               mAccuracyZ = z;  
       }  
       public float getAccuracyX() {  
               return mAccuracyX;  
       }  
       public float getAccuracyY() {  
               return mAccuracyY;  
       }  
       public float getAccuracyZ() {  
               return mAccuracyZ;  
       }  
 
       public boolean isReady() {  
               return (mSensor == null) ? false : true;  
       }  
       public void start() {  
               if (isReady()) {  
                       mSensorManager.registerListener(mSensorListener, mSensor, mRate);  
               } else {  
                       mBrowser.exception(getInterfaceName(), "Accelerometer is not found.");  
               }  
       }  
       public void stop() {  
               if (isReady()) {  
                       mSensorManager.unregisterListener(mSensorListener);  
               }  
       }  
 
}


搜索更多相关主题的帖子: 传感器 2011 Android Copyright 加速度 
2011-12-02 17:39
绿茶盖儿
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:1
帖 子:363
专家分:1852
注 册:2011-9-3
收藏
得分:7 
不错,学习了
2011-12-03 11:29
Alar30
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:10
帖 子:988
专家分:1627
注 册:2009-9-8
收藏
得分:0 
来看代码了
学习!
2011-12-29 19:37
剑魔风幻
Rank: 2
来 自:山东
等 级:论坛游民
帖 子:8
专家分:10
注 册:2011-11-20
收藏
得分:0 
飘过
2011-12-30 10:55
catsbee
Rank: 2
等 级:论坛游民
帖 子:8
专家分:24
注 册:2011-8-29
收藏
得分:0 
看不懂啊~
2011-12-30 22:44
l253506241
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2013-9-19
收藏
得分:0 
看不懂啊~
2013-09-19 22:35
快速回复:Android传感器API之:加速度Accelerometer功能源码
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.044840 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved