SyntaxBlitz / PluckLock

Immediately lock your phone if someone snatches it from your hand
MIT License
24 stars 7 forks source link

Figure out some magical way to prevent the SensorManager from polling the accelerometer while the phone is locked #3

Open SyntaxBlitz opened 9 years ago

kuleszdl commented 9 years ago

Okay, but normally when you open a whole application which uses the sensor and close it the sensor is not used anymore, right? Did you try to maybe fork a new thread/process which does the polling and kill/spawn it as required? Maybe this way you could overcome the non-working SensorManager?

SyntaxBlitz commented 9 years ago

I did consider this but never actually tried writing it. I don't think I know enough about Android's process management, nevermind inter-process communication, to successfully write that code.

Already, the polling code is inside a Service. When I get the phone lock event, I kill the entire Service, which is supposed to kill the SensorManager in its onDestroy method (this is the bit that doesn't work). Even though the Service is dead, I still get values from the sensor.

When the Service is not running, the application is doing nothing (assuming the settings Activity is not open). It has registered its BroadcastReceiver for the lock/unlock events, and then it is totally passive. Spawning another process and controlling that one would probably work, I agree. But it seems silly that, given that I'm killing the Service when it's not in use, that's not already what's happening. I also worry, in spawning another process, about the processes running out of sync. The worker process might be killed prematurely, and it might also take extra time to start up once the main app gets the unlock event. Most of the code would actually be inside this worker process, so it just seems weird that I'd even need two processes in the first place.

This issue definitely frustrates me. If I get time, I'll look at how I can spawn a new process. I'd just have to hope that that works.

SyntaxBlitz commented 9 years ago

Here I'll leave the link to the StackOverflow question I've posted.

I did mention in the last paragraph that I was looking for alternative methods, like those involving process management. I don't know quite how successful I'll be.