如何停止的处理程序? [英] How to stop the handler?

查看:139
本文介绍了如何停止的处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个疑问..具有活性的左撇子在我的应用程序PM。虽然我的活动破坏了处理程序仍在运行。难道是运行在不同的工艺应用过程中的其他?难道任何一个PLZ解释为什么它的工作,像这样? 是否有可能停止处理程序,而活动的onDestroy?

I have a doubt .. Am having a hander in an activity in my application. Though my activity destroyed the handler still functioning. Is it running on different process other than the application process ? Could any one plz explain why its working like so ? Is it possible to stop the handler while onDestroy of the activity ?

在此先感谢。

推荐答案

由于文档中描述

<一个href="http://developer.android.com/reference/android/os/Handler.html">http://developer.android.com/reference/android/os/Handler.html

http://developer.android.com/reference/android/os/Handler.html

每一个处理程序实例都与一个单独的线程和线程的消息队列相关联。

"Each Handler instance is associated with a single thread and that thread's message queue."

当你即将结束你的活动,如:在的onDestroy()您还需要取消回调可运行它启动了:

When you are about to finish your activity, e.g. in onDestroy() you also need to cancel the callback for the runnable it was started for:

mHandler.removeCallbacks(previouslyStartedRunnable);

您可以做,即使没有,如果可运行已经解雇了,而你的行为是积极的检查。

You can do that even without checking if runnable was already fired while your activity was active.

更新:

有需要考虑两个附加箱子:

There are two additional cases to be considered:

1)你已经实现在你的Runnable接口,如创建新类的方式处理程序

1.) You have implemented your Handler in a way that you created new class for the Runnable, e.g.

private class HandleUpdateInd implements Runnable...

通常你需要做的,如果你要开始延迟可运行当前的参数设置(可能改变,直到可运行火灾)。要取消它,你需要使用

Usually you need to do that if you have to start delayed runnable with current set of parameters (which may change until runnable fires). To cancel it you need to use

mHandler.removeCallbacksAndMessages(HandleUpdateInd.class);

2)。如果您使用的是内联调用(JPM感谢您的评论)

2.) If you are using inline call (JPM thanks for the comment)

handler = new Handler() { public void handleMessage(Message msg) { ... } };

然后,你需要定义什么价值的信息。后来,如果您需要取消它,你可以使用

Then you need to define "what" value for that Message. Later on, if you need to cancel it you can use

handler.removeMessages(what); 

要执行这项任务。

这篇关于如何停止的处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆