0

I made an app that reads values off of the proximity sensor and performs certain actions based off of that data. My problem is that the actions will continue to perform even when the user presses the home button and leaves the app, thus causing a drainage in the battery.

My question is how do I turn off the proximity sensor once the app is existed or simple when the home button is pressed (without the app being exited properly)

This is my first Android app. Here is how I assign the proximity sensor in my code:

    sm = (SensorManager) getSystemService(SENSOR_SERVICE);
    proxSensor = sm.getDefaultSensor(Sensor.TYPE_PROXIMITY);

    sm.registerListener(this,proxSensor,SensorManager.SENSOR_DELAY_NORMAL);

Thanks

EDIT To those that are wondering and don't want to look it up, I solved it by using the following:

public void onPause()
{
    super.onPause();
    sm.unregisterListener(this);
}
public void onResume()
{
    super.onResume();
    sm.registerListener(this,proxSensor,SensorManager.SENSOR_DELAY_NORMAL);
}
Mo2
  • 1,090
  • 4
  • 13
  • 26

2 Answers2

3

Look into the Activity method onStop(). It's called whenever the Activity is no longer visible, (app is exited or home button is pressed). It is into this method where you will put your code to stop the proximity sensor.

See Android Activity lifecycle diagram here for more info:

http://developer.android.com/reference/android/app/Activity.html

Martin Konecny
  • 57,827
  • 19
  • 139
  • 159
  • So to stop the proximity sensor, do I just use uregisterListener? – Mo2 May 26 '14 at 03:42
  • Yes exactly as outlined on the bottom of the page here: http://developer.android.com/guide/topics/sensors/sensors_position.html – Martin Konecny May 26 '14 at 03:44
  • Alright, thank you. Now would it be safe to just use onPause() instead of both, onPause and onStop? All I need is to unregister the sensor if the app goes in the background or something else comes up on top of it. Would onPause and onResume be enough to cover both? – Mo2 May 26 '14 at 03:51
  • onPause is called when something like the Androud pull down notification shade partially covers your activity. onStop is called when the Activity is completely gone from view. It's up to you whether you use onPause or onStop, but choose only one. – Martin Konecny May 26 '14 at 03:54
  • So would onPause also take over when the home button is pressed for example, or is strictly when something pops up in the foreground. Also, why only choose one? – Mo2 May 26 '14 at 04:03
  • The lifecycle is defined in that it's guaranteed that if onStop is called, then onPause will be called before it. – Martin Konecny May 26 '14 at 04:04
  • 1
    I see. Thank you! One last comment. I just tried onPause and pulling down the android drop down did not stop it. You said it would. – Mo2 May 26 '14 at 04:33
  • 1
    I may be mistaken about the notification bar - by definition Activity A `onPause()` is called when When activity B is launched in front of activity A. I've recreated this in the past if you have a system dialog box pop-up asking to enable Bluetooth see this: http://developer.android.com/images/bt_enable_request.png – Martin Konecny May 26 '14 at 14:42
1

Apps don't exit on Android. At most Activities will finish. But to turn off behavior when another activity is active, turn it off in the activity's onPause.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127