简体   繁体   English

有关Android上的焦点和活动的问题

[英]Question about focus and activites on Android

I have a chat application here that uses a handler and a runnable to frequently poll for updates. 我这里有一个聊天应用程序,它使用处理程序和Runnable频繁轮询更新。 On receiving an update as HTML, it adds this content to the TextView in the main activity. 收到HTML格式的更新后,它将在主活动中将此内容添加到TextView中。

Now, I've noticed that when the activity loses focus and I use other applications/view the home screen, the runnable keeps going on and when I re-open the application, I see the TextView with the updated content. 现在,我注意到,当活动失去焦点并且我使用其他应用程序/查看主屏幕时,可运行项继续运行,并且当我重新打开应用程序时,我看到了包含更新内容的TextView。

Is this considered unsafe and should I build a service to do this, or is it fine to modify the UI when it's unfocused like that? 这是否被认为是不安全的,我应该建立一个服务来做到这一点,还是在没有重点时修改UI很好?

I don't believe that it's good for your applicaation to update the ui when it's not focused as the resources on some devices might be limited and the battery life will also be reduced because of the permanent cpu and data connection usage. 我不认为在不集中用户界面的情况下更新ui对您的应用程序没有好处,因为某些设备上的资源可能会受到限制,并且由于永久使用cpu和数据连接的使用,还会缩短电池寿命。

To implement this as a server seems to be overkill. 将其实现为服务器似乎是过分的。 Why don't you clear the handler queue with removeMessage when the onPause of the activity is called and fill it again during the onResume? 为什么在调用活动的onPause时不使用removeMessage清除处理程序队列,并在onResume期间再次填充它?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM