views:

78

answers:

1

I have a very tough problem I can't solve. I have an app which I can put into airplane mode remotely to save power. I have a thread running which gets the power-save message, removes callbacks for all other threads, puts the phone into airplane mode, and waits a specified number of minutes before waking the phone back up.

When the phone is plugged into my laptop (USB debugging enabled, etc) then this functionality is fine. The phone goes into airplane mode, the screen locks, and then after a specific number of minutes the app "wakes up" and continues as normal.

When the phone is plugged into a car charger, the phone phone goes into airplane mode, but the thread never wakes up the phone. This problem only occurs when the screen is also set to timeout...when I set the screen to "never timeout" and have the phone plugged into a car charger, the app wakes up just fine.

It appears that some combination of power source and the screen timing out kills my power-save thread at a system level. I know this post is very specific to my application, but has anyone encountered a problem similar to this before?

+1  A: 

If you want to wait n number of minutes, you need to use an alarm, not a thread. You can never assume that a thread remains active while your application is not in the foreground. Check out http://developer.android.com/reference/android/app/AlarmManager.html.

EboMike
Great thanks, I'll try this out!
Tom G
So, if I use my alarm to launch an activity after 'n' number of minutes, how could I get that activity to call the function in my parent class to 'restart all threads'?
Tom G
You define an intent in your alarm (via PendingIntent). This intent can be anything you want, and it could be a special intent that tells the app to restart the threads.
EboMike