Keep UI Thread running when screen is off
I'm making an app that updates th开发者_运维问答e UI through the runOnUiThread(Runnable) method. It is very important that this UI thread continues to run as long as the user has this app running, even when onPause(). However, I've realized that when the screen is locked, Android stops the UI thread; and I don't blame them, it's probably to save battery life. But I want to know if there is a way to override this and tell the activity or the system to keep running the UI thread.
I would rather run your code as a Service, and hook it to the Activity having the UI through bindService.
The other option you have, is to create a WakeLock and maintain the phone on all the time. It may be very hard to the battery, though.
Check the dev guide entry for bound services and see if it fits your design: http://developer.android.com/guide/topics/fundamentals/bound-services.html
There are many very good tutorials out there for running services, with varying degrees of complexity. Take a look at some of them here:
- http://www.vogella.de/articles/AndroidServices/article.html
- http://marakana.com/forums/android/examples/60.html
- http://developerlife.com/tutorials/?p=356
- http://mylifewithandroid.blogspot.com/2008/02/double-life-of-service.html
精彩评论