Android Handler的使用
这是使用处理程序的更好方法。有什么优点。我遇到的所有示例似乎都给出了内联版本。
在类中使用implementsHandler.Callback
并实现接口方法。
或
使用内联代码版本
private Handler mHandler = new Handler(){ ....};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这些内联类定义的常用术语是匿名类。
您可以在Java/Android:匿名本地类与命名类中阅读有关这些内容的更多讨论
本质上,主要区别在于可读性、编码速度、重用和范围。
从资源的角度来看,匿名类创建可能会导致垃圾收集器产生开销,如 避免创建不必要的对象。我不确定匿名类创建的确切细节,但是,在类上实现接口更有效是合乎逻辑的。
@WilliamTMallard 提供了一个不该做什么的示例。在他的示例中,应该在类上实现一个长且语法复杂的处理程序,而不是匿名处理程序,因为在内联定义时更难以读取和编辑。
The common term or these inline class definitions is Anonymous Classes.
You can read more about the discussion on these in Java/Android: anonymous local classes vs named classes
Essentially the main differences are readbility, speed of coding, re-use and scope.
From a resource point of view the anonymous class creation may cause an overhead in the garbage collector as discussed in Avoid Creating Unnecessary Objects. I am not certain on the exact details of anonymous class creation, however, it is logical that implementing the interface on the class is more efficient.
@WilliamTMallard has provided an example of what NOT to do. In his example, a long and syntacticly complex handler should be implementented on the class rather than anonymous handler because it is harder to read and edit when defined inline.
http://developer.android.com/reference/android/os/Handler。 html
处理程序允许您发送和处理与线程的 MessageQueue 关联的 Message 和 Runnable 对象。每个 Handler 实例都与一个线程和该线程的消息队列相关联。当您创建一个新的处理程序时,它会绑定到创建它的线程的线程/消息队列 - 从那时起,它将向该消息队列传递消息和可运行对象,并在它们从消息中出来时执行它们队列。
处理程序有两个主要用途:
将来;并将
你自己的。
示例 1
在应用启动页面中使用处理程序。
示例2
如果请求工作可能耗时,请在子线程中使用Handler请求网络。
示例3
使用Handler和Timer来更新进度条。
http://developer.android.com/reference/android/os/Handler.html
A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue. Each Handler instance is associated with a single thread and that thread's message queue. When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it -- from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue.
There are two main uses for a Handler:
in the future; and
your own.
Exmaple 1
use handler in app splash page.
Example 2
use Handler request network in child thread if the request work may time consuming.
Example 3
use Handler and Timer to update progress bar.
这确实不是上述问题的答案,因为我不知道“最好的方法”是什么,这可能取决于你在做什么。不过,我会解释我在做什么以及为什么。
我正在编写一个用作遥控器的应用程序。有多种活动将与受控设备交互,并且根据命令的结果及其来自的活动需要发生不同的事情。我不喜欢处理程序的两件事是:A)它们最终成为一种“厨房水槽”构造,实现来自不同来源的功能;B)它们分离了一个操作(在我的例子中是命令的发送)来自该操作结果的处理。然而,使用匿名(正确的术语?我真是个菜鸟。)处理程序作为参数允许我将逻辑保持在一起。这是我的方法的伪代码:(
永远记住,这可能完全值得您为此付出的代价。;))
This really isn't an answer to the above question because I don't know what "the best way" is, and it likely depends on what you're doing. However, I'll explain what I'm doing and why.
I'm writing an app that serves as a remote controller. There are several activities that will interact with the controlled device, and different things need to happen based on the result of the command and the activity it came from. Two things I didn't like about handlers are A) that they end up being a sort of "kitchen sink" construct, implementing functionality from different sources, and B) that they separated an action (the send of the command in my case) from the processing of the result of that action. However, using an anonymous (right term? I'm such a noob.) handler as a parameter allows me to keep the logic together. Here's the pseudocode for my approach:
(Always remember, this is probably worth exactly what you've paid for it. ;) )
在 Android 中使用匿名类存在危险。如 这篇博文中所述< /a> -
泄密的机会来了。
因此,简短的答案是:实现接口方法或使用静态内部类(不保存外部类引用)。
例如,泄漏安全处理程序可能如下所示:
我制作了一个 关于处理程序的使用的博客文章,所以可能也值得检查:)
There is a danger in using anonymous classes in Android. As described in this blog post -
And here comes an opportunity for a leak.
So, the short answer would be: implement the interface methods or use static inner classes (which don't hold an outer class reference).
For instance, a leak-safe Handler could look like this:
I made a blog post around usage of Handlers, so might be worth checking as well :)