Handler vs Thread

六眼飞鱼酱① 提交于 2020-01-12 04:40:35

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/3102203/handler-vs-thread

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!