简体   繁体   English

从辅助线程访问Android UI工具包

[英]Accessing Android UI toolkit from worker thread

According to Android doc, "...Additionally, the Andoid UI toolkit is not thread-safe. So, you must not manipulate your UI from a worker thread—you must do all manipulation to your user interface from the UI thread. Thus, there are simply two rules to Android's single thread model: 根据Android文档,“ ...此外,Andoid UI工具包也不是线程安全的。因此,您不能从辅助线程中操作UI,而必须从UI线程对用户界面进行所有操作。因此, Android的单线程模型只有两个规则:

Do not block the UI thread Do not access the Android UI toolkit from outside the UI thread" 不要阻止UI线程不要从UI线程外部访问Android UI工具包”

That seems very plausible, but is it correct that the compiler does NOT complain at all if a programmer violates the second rule? 这似乎很合理,但是如果程序员违反了第二条规则,那么编译器根本不会抱怨是正确的吗?

Thank you. 谢谢。

You will not get a compile-time error. 您不会收到编译时错误。 You may (or may not) get a run-time error. 您可能会(或可能不会)收到运行时错误。

Can you tell me the situation where you need to access UI toolkit from the thread ? 您能告诉我需要从线程访问UI工具箱的情况吗? There are many ways to update UI like 有很多方法可以更新UI,例如

  1. Handler 处理程序
  2. AsyncTask 异步任务
  3. runOnUiThread method of View View的runOnUiThread方法
  4. BroascastReceiver 广播广播接收器

I hope this help 我希望这个帮助

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

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