package com.notificationFramework.stimulusStrategy; 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.support.annotation.Nullable; /** * Created by pharmacy on 26/06/2018. */ public class ProximitySensor extends Service implements SensorEventListener { static float proximity; /** * The method which is called when the ProximitySensor service is started. Sets up the listener to * be registered which remains constantly registered. * * @param intent the intent used to start this service * @param flags additional information about this service * @param startId the unique identifier for this service * @return the conditions under which the OS should treat this service * @see android.app.Service */ @Override public int onStartCommand(Intent intent, int flags, int startId) { SensorManager mSensorManager = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE); Sensor proximity = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY); mSensorManager.registerListener(this, proximity, 600000000); return START_STICKY; } @Nullable @Override public IBinder onBind(Intent intent) { return null; } /** * Changes the field values when a the proximity sensor reports a change. * * @param event the sensor event which occurred */ @Override public void onSensorChanged(SensorEvent event) { proximity = event.values[0]; } @Override public void onAccuracyChanged(Sensor sensor, int i) { } }