• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How to Make Sure Accelerometer is Off?

SoulMazer

Lurker
Hi, I'm working on an app that takes advantage of the accelerometer. I use the SensorEventListener library to do this. However, I have had a problem where I believe the accelerometer did not shut off with my application. I discovered this because after I was working on/debugging my application for a while, I went to the store, and my phone's battery was drained quite a bit by the time I got home. I looked at what was draining my battery, and it was my application, even though I wasn't even running it (it didn't show up in the task manager).

I override the function "onStop()" with the following code:
Code:
super.onStop();
       sensorManager.unregisterListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER));
       sensorManager.unregisterListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION));

Will this always turn off the accelerometer when my app exits? Was that one occurrence just a freak accident or should I do something else to ensure that the accelerometer is turned off?

Thanks in advance.
 
I'm still having the same general problem, but I would also like to expand it with a more general question. I've done some reading, and am a little mystified about what happens when you want to exit an app. From what I've concluded, it is the Android system that determines when your app gets destroyed. If this is true...how can you ensure that your app is able to run "clean up" functions?

This all pretty much applies to my battery problem. I have a feeling that because of my confusion with the activity life-cycle, my app is not being properly ended. If I play my game for, say, two or three minutes, close it, then do other things or lock the screen, when I come back, I look at my battery and it is drained fairly significantly. I know this is due to my app because when I look at the Android power manager, it says that 97% of my power usage is due to my app. When I click (or tap, if you may) on this for more information, the "Force Close" option is grayed-out, which gives me the idea that my app is in some sort of limbo.

Also, as I previously noted, my app uses the accelerometer. After doing a little research, I've discovered that the accelerometer is a pretty big battery hog. It's only a guess, but I think that my app never fully turns off the accelerometer.

So, could anybody help explain to me the end of the activity cycle a little better, and how to ensure that my app truly exits, instead of eating up my battery?

Thanks in advance.
 
Back
Top Bottom