处理程序与线程

发布于 2024-09-06 14:20:13 字数 303 浏览 7 评论 0原文

我想知道一次。我读过很多地方。当我想做一些“长时间操作”时,我应该使用Handler

但我不明白为什么?我所有的“长时间操作”都用常规线程包围,并且工作正常。

为什么我要使用 Handler 来实现此目的?

我唯一一次必须使用 Handler 是当我必须安排一些任务时(postDelayed

我是否想念关于 handlers 的主要想法(什么时候我应该真正使用它)?或者也许并没有真正的区别?

I would like to know, once for all. I've read in many places. When I want do some 'long time operations' I should use a Handler.

But I don't get why? All my 'long-time-operations' I surround with a regular threads, and it works fine.

Why would I use Handler for this?

The only time I had to use Handler was, when I had to schedule some task(postDelayed)

Is there any main idea I miss about handlers(When I should really use it)? Or maybe there isn't really difference?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜扑 2024-09-13 14:20:13

Handler 允许您从后台线程与 UI 线程进行通信。这是因为后台线程中禁止进行 UI 操作。请注意,从版本 1.5 开始, AsyncTask 类使得更容易这样做。

A Handler lets you communicate back with the UI thread from your background thread. This is because UI operations are forbidden from within background threads. Note that starting at version 1.5, the AsyncTask class makes it much easier to do so.

葬シ愛 2024-09-13 14:20:13

它不仅仅是让您返回 UI 线程,因为 runOnUiThread(Runnable) 做得非常好。我怀疑这更多的是为了让 Android 更容易管理不应存在于 Activity 上下文之外的线程和其他资源,并且“Activity 已泄漏...”异常会告诉您何时发生这种情况。

It can't just be about getting you back to the UI thread since runOnUiThread(Runnable) does that very nicely. I suspect this is more about making it easier for Android to manage threads and other resources that shouldn't live outside of an Activity's context, and that the "Activity has leaked..." exceptions tell you when that's happened.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文