保持UI线程运行时,屏幕关闭 [英] Keep UI Thread running when screen is off

查看:149
本文介绍了保持UI线程运行时,屏幕关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个应用程序,通过runOnUiThread(Runnable的)方法更新用户界面。这是非常重要的,这个UI线程继续,只要用户有这个程序运行时需要运行,即使在onPause()。不过,我意识到,当屏幕被锁定,停止的Andr​​oid UI线程;我不怪他们,它可能以延长电池寿命。但我想知道是否有一种方法来覆盖这一点,并告知活动或系统保持运行UI线程。

I'm making an app that updates the 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.

推荐答案

我宁愿运行code作为一种服务,并把它挂到具有通过<一个用户界面的活动href="http://developer.android.com/reference/android/content/Context.html#bindService%28android.content.Intent,%20android.content.ServiceConnection,%20int%29"相对=nofollow> bindService

I would rather run your code as a Service, and hook it to the Activity having the UI through bindService.

你有另一种选择,就是创建一个 WakeLock 并保持在手机上所有的时间。这可能是非常困难的电池,虽然

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.

检查绑定服务的开发指南条目,看看它是否适合你的设计:的 http://developer.android.com/guide/topics/fundamentals/bound-services.html

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

这篇关于保持UI线程运行时,屏幕关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆