Android感應器API:近距離感應Proximity

來源:互聯網
上載者:User

/* 
* 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.JavascriptInte**ce; 
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 RexseeSensorProximity implements JavascriptInte**ce { 

private static final String INTE**CE_NAME = "Proximity"; 
@Override 
public String getInte**ceName() { 
return mBrowser.application.resources.prefix + INTE**CE_NAME; 

@Override 
public JavascriptInte**ce getInheritInte**ce(RexseeBrowser childBrowser) { 
return this; 

@Override 
public JavascriptInte**ce getNewInte**ce(RexseeBrowser childBrowser) { 
return new RexseeSensorProximity(childBrowser); 

public static final String EVENT_ONPROXIMITYCHANGED = "onProximityChanged"; 

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; //milliseconds 
private int mEventCycle = 100; //milliseconds 
private float mAccuracy = 0; 

private long lastUpdate = -1; 
private long lastEvent = -1; 

private float value = -999f; 

public RexseeSensorProximity(RexseeBrowser browser) { 
mContext = browser.getContext(); 
mBrowser = browser; 
browser.eventList.add(EVENT_ONPROXIMITYCHANGED); 

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

mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY); 

mSensorListener = new SensorEventListener() { 
@Override 
public void onAccuracyChanged(Sensor sensor, int accuracy) { 

@Override 
public void onSensorChanged(SensorEvent event) { 
if (event.sensor.getType() != Sensor.TYPE_PROXIMITY) return; 
long curTime = System.currentTimeMillis(); 
if (lastUpdate == -1 || (curTime - lastUpdate) > mCycle) { 
lastUpdate = curTime; 
float lastValue = value; 
value = event.values[SensorManager.DATA_X]; 
if (lastEvent == -1 || (curTime - lastEvent) > mEventCycle) { 
if (Math.abs(value - lastValue) > mAccuracy) { 
lastEvent = curTime; 
mBrowser.eventList.run(EVENT_ONPROXIMITYCHANGED); 




}; 

public String getLastKnownValue() { 
return (value == -999) ? "null" : String.valueOf(value); 

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 value) { 
mAccuracy = Math.abs(value); 

public float getAccuracy() { 
return mAccuracy; 

public boolean isReady() { 
return (mSensor == null) ? false : true; 

public void start() { 
if (isReady()) { 
mSensorManager.registerListener(mSensorListener, mSensor, mRate); 
} else { 
mBrowser.exception(getInte**ceName(), "Proximity sensor is not found."); 


public void stop() { 
if (isReady()) { 
mSensorManager.unregisterListener(mSensorListener); 

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.