How can I run this Android code in Background
<p>This is a actually a collage project. The requirements are:</p>

<ol>
<li>Make an app that will change Android Profile to Ringer, Vibration and Silent using any two sensors (I used Proximity and Accelerometer).</li>
<li>Make sure the app runs in Background even after App is closed.</li>
<li>Continuously running sensors consumes too much battery, Do something about is so battery power can be saved as much as possible.</li>
</ol>

<p>I already fulfilled NO: 1 and functioning as expected, Just 2 and 3 remains. 
What will be the easiest way to run this code in Background: I have an idea like this:</p>

<p><a href="https://i.stack.imgur.com/NoQi1.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NoQi1.jpg" alt="enter image description here"></a></p>

<p>I want to Start and Stop the Background service using the Two Buttons.</p>

<p>Here is the Code for NO: 1.</p>

<pre><code>public class SensorActivity extends Activity implements SensorEventListener{

private SensorManager mSensorManager;
private Sensor proxSensor,accSensor;

private TextView serviceStatus,profileStatus;
private Button startService,endService;

private boolean isObjectInFront,isPhoneFacedDown;

private AudioManager audioManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sensor);

    audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

    mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    proxSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
    accSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

    isObjectInFront = false;
    isPhoneFacedDown = false;

    serviceStatus = (TextView) findViewById(R.id.textView_serviceStatus);
    profileStatus = (TextView) findViewById(R.id.textView_profileStatus);
}

protected void onResume() {
    super.onResume();
    mSensorManager.registerListener(this, proxSensor, SensorManager.SENSOR_DELAY_NORMAL);
    mSensorManager.registerListener(this, accSensor, SensorManager.SENSOR_DELAY_NORMAL);
}


protected void onPause() {
    super.onPause();
    mSensorManager.unregisterListener(this);
}

@Override
public void onSensorChanged(SensorEvent event) {

    if (event.sensor.getType() == Sensor.TYPE_PROXIMITY) {
        if(event.values[0] &gt; 0){
            isObjectInFront = false;
        }
        else {
            isObjectInFront = true;
        }

    }
    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        if(event.values[2] &lt; 0){
            isPhoneFacedDown = true;
        }
        else {
            isPhoneFacedDown = false;
        }
    }

    if(isObjectInFront &amp;&amp; isPhoneFacedDown){
        audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
        profileStatus.setText("Ringer Mode : Off\nVibration Mode: Off\nSilent Mode: On");
    }
    else {
        if(isObjectInFront){
            audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
            profileStatus.setText("Ringer Mode : Off\nVibration Mode: On\nSilent Mode: Off");
        }
        else {
            audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
            profileStatus.setText("Ringer Mode : On\nVibration Mode: Off\nSilent Mode: Off");
        }
    }



}

@Override
public void onAccuracyChanged(Sensor sensor, int i) {

}
</code></pre>

<p>}</p>