I want to use proximity sensor in my project. I searched for proximity sensor tutorial. And I found a sensor tutorial at http://www.vogella.com/articles/AndroidSensor/articl
The right way to use proximity sensor is via PowerManager. Forget about light sensor and use Android abstraction and already written code!
public class ProximityViaPowerManager {
private Context context;
private WakeLock proximityWakeLock;
public ProximityViaPowerManager(Context context) {
this.context = context;
}
public boolean enableProximityWakeLock() {
if (proximityWakeLock != null) {
return true;
}
PowerManager powerManager =
context.getApplicationContext().getSystemService(Context.POWER_SERVICE);
proximityWakeLock = powerManager.newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, TAG);
proximityWakeLock.acquire();
return true;
}
public void disableProximityWakeLock() {
if (proximityWakeLock != null) {
proximityWakeLock.release();
proximityWakeLock = null;
}
}
}
Ensure correct enable/disable capability in Android life cycle resume/pause and add the permission to manifest: