删除回调处理程序无法正常工作 [英] Remove callback for handler not work

查看:185
本文介绍了删除回调处理程序无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写调用该方法,每次间隔的处理程序。我想在删除处理程序上的destroy()。在code I的使用如下,在OnCreate()

I have written a handler that calls the method every time interval. I want to remove that handler in on destroy(). The code i use as follows, In Oncreate()

private final Handler _handler = new Handler();
public int DATA_INTERVAL = 30 * 1000;
Runnable getData;
 getData = new Runnable()
        {
            @Override
            public void run()
            {
                     recieveData();
            }
        };

 _handler.postDelayed(getData, DATA_INTERVAL);

和中的OnDestroy(),我使用,

and in ondestroy(), i use,

_handler.removeCallbacks(getData);

但removecallbacks无法正常工作。它退出活动后调用。

But removecallbacks not work. It calls after exiting the activity.

推荐答案

removeCallbacks(Runnable接口R)

删除在消息队列的Runnable为r的任何未决的帖子。

Remove any pending posts of Runnable r that are in the message queue.

所以 removeCallbacks(..)只停未决消息(的Runnable)目前没有运行可运行的,所以如果要停止正在运行的可运行的,然后使用布尔varaible的回采主题当用户退出从您的应用程序。

so removeCallbacks(..) only stops pending messages (Runnables) not currently running runnable so if you want to stop currently running Runable then use a Boolean varaible for Stoping Thread when user Exit from your app.

看到这个帖子的<一个href=\"http://stackoverflow.com/questions/5844308/removecallbacks-not-stopping-runnable\">removeCallbacks没有阻止可运行

这篇关于删除回调处理程序无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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