Skip to content

Instantly share code, notes, and snippets.

@lkorth
Last active December 12, 2023 15:52
Show Gist options
  • Save lkorth/4513895 to your computer and use it in GitHub Desktop.
Save lkorth/4513895 to your computer and use it in GitHub Desktop.
Simple Android service that will turn the screen on when the proximity sensor reports a object is close to the device.
<uses-permission android:name="android.permission.WAKE_LOCK" />
<service android:name=".ProxService" />
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.IBinder;
import android.os.PowerManager;
public class ProxService extends Service implements SensorEventListener {
@Override
public void onCreate() {
SensorManager sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
Sensor proximitySensor = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
if (proximitySensor == null)
stopSelf();
else
sensorManager
.registerListener(this, proximitySensor, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_PROXIMITY) {
if (event.values[0] < event.sensor.getMaximumRange()) {
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK
| PowerManager.ACQUIRE_CAUSES_WAKEUP, "CHESS");
wl.acquire();
try {
Thread.sleep(30 * 1000); // 30 seconds
} catch (Exception e) {
} finally {
wl.release();
}
}
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment