並不是所有Android手機上都保留有距離感應器。 基於這個感應器可以有一些很不錯的小應用,比如近距離感應鎖屏、解鎖......

分享Rexsee的距離感應功能源碼,回頭可以自個兒做。。 更多的感應器API我這幾天都有陸續發出,大家可以自己搜「Android感應器API之......」,

SensorProximity.java全部源碼:

[代碼] JAVA代碼

/*

* 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;

import android.content.CoNtext;

import android.hardware.Sensor;

import android.hardware.SensorEvent;

import android.hardware.SensorEventListener;

import android.hardware.SensorManager;

public class RexseeSensorProximity implements JAVAscriptInterface {

private static final String INTERFACE_NAME = "Proximity";

@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 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(getInterfaceName(), "Proximity sensor is not found.");

}

}

public void stop() {

if (isReady()) {

mSensorManager.unregisterListener(mSensorListener);

}

}

}
創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()