安卓:定时器/延迟替代 [英] Android: Timer/Delay Alternative

查看:200
本文介绍了安卓:定时器/延迟替代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打一个形象是对于看得到60毫秒,然后是无形的,那么我想另一个图像做相同的..等等。 我不认为我使用的定时器的权利..因为当我运行应用程序两个图像在同一时间打开,当我preSS使用此功能按键不会消失。

下面是一些示例code ..

  timer.schedule(新的TimerTask()
        {
            @覆盖
            公共无效的run()
            {
                LED_1.setVisibility(View.VISIBLE);
                                    // LED_1是ImageView的
            }
        },60);
        LED_1.setVisibility(View.INVISIBLE);timer2.schedule(新的TimerTask()
        {
            @覆盖
            公共无效的run()
            {
                LED_2.setVisibility(View.VISIBLE);
                                    // LED_2是ImageView的
            }
        },60);
        LED_2.setVisibility(View.INVISIBLE);

难道还有其他选择吗?我已经试过类似的例子..
<一href=\"http://stackoverflow.com/questions/4562269/android-app-how-to-delay-your-service-start-on-phone-boot\">Android应用程序如何推迟的手机开机广告服务启动

http://www.roseind​​ia.net/java/beginners/DelayExample.shtml

但它没有做我想要的。

什么我做错了吗?还是有另一种方式,我可以做到这一点?

感谢。

-Faul

有关Good.Dima ..

  INT delayRate = 60;
        最终的Runnable LED_1_On =新的Runnable()
    {
        公共无效的run()
        {
            LED_1.setVisibility(View.VISIBLE);
                    handler.postDelayed(这一点,delayRate);        }
    };    handler.postDelayed(LED_1_On,delayRate);    最终的Runnable LED_2_On =新的Runnable()
    {
        公共无效的run()
        {
            LED_1.setVisibility(View.INVISIBLE);
            LED_2.setVisibility(View.VISIBLE);
                    handler3.postDelayed(这一点,delayRate);        }
    };    handler.postDelayed(LED_2_On,delayRate);


解决方案

您可以尝试用处理器,它的帖子水木清华到UI线程,它可以延迟发布
 <一href=\"http://developer.android.com/reference/android/os/Handler.html#postDelayed%28java.lang.Runnable,%20long%29\"相对=nofollow> postDelayed

I want to make an image be visibile for 60 ms and then be invisible, then I want another image to do the same.. and so on. I don't think I'm using the Timer right.. because when I run the app both images turn on at the same time and don't disappear when I press the button that uses this function.

Here's some sample code..

timer.schedule(new TimerTask()
        {
            @Override
            public void run()
            {
                LED_1.setVisibility(View.VISIBLE);
                                    // LED_1 is an ImageView
            }
        }, 60);
        LED_1.setVisibility(View.INVISIBLE);

timer2.schedule(new TimerTask()
        {
            @Override
            public void run()
            {
                LED_2.setVisibility(View.VISIBLE);
                                    // LED_2 is an ImageView
            }
        }, 60);
        LED_2.setVisibility(View.INVISIBLE);

Is there another alternative? I've tried examples like.. Android app How to delay your Service start on phone boot

and

http://www.roseindia.net/java/beginners/DelayExample.shtml

But it's not doing what I want..

Anything I'm doing wrong? Or is there an alternative way that I can do this?

Thanks.

-Faul

For Good.Dima..

            int delayRate = 60;
        final Runnable LED_1_On = new Runnable()
    {
        public void run()
        {
            LED_1.setVisibility(View.VISIBLE);
                    handler.postDelayed(this, delayRate);

        }
    };

    handler.postDelayed(LED_1_On, delayRate);

    final Runnable LED_2_On  = new Runnable()
    {
        public void run()
        {
            LED_1.setVisibility(View.INVISIBLE);
            LED_2.setVisibility(View.VISIBLE);
                    handler3.postDelayed(this, delayRate);

        }
    };

    handler.postDelayed(LED_2_On, delayRate);

解决方案

You can try to use Handler, it posts smth into UI thread, it can post with a delay postDelayed

这篇关于安卓:定时器/延迟替代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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