码迷,mamicode.com
首页 > 移动开发 > 详细

[Android]传感器-加速度计-1[转]

时间:2014-06-08 07:38:06      阅读:363      评论:0      收藏:0      [点我收藏+]

标签:android   c   class   code   java   a   

 

/* * 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.rexsee.com/CN/legal/license.html * * 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; //具体参考http://www.rexsee.com/CN/helpReference.php 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); } } }

 

[Android]传感器-加速度计-1[转],布布扣,bubuko.com

[Android]传感器-加速度计-1[转]

标签:android   c   class   code   java   a   

原文地址:http://www.cnblogs.com/webapplee/p/3774021.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!