简体   繁体   English

Android处理程序/计时器请求

[英]Android Handler/Timer Request

So I have a question, and if it's a stupid one I do apologize up front, I have tried to search for it but not sure what to search for exactly. 因此,我有一个问题,如果是一个愚蠢的问题,我会事先道歉,我曾尝试搜索它,但不确定确切要搜索什么。 I am trying to run a delayed task, but only if my int = 0, would this work correctly like I am wanting it to? 我正在尝试执行延迟的任务,但是只有当我的int = 0时,才能像我希望的那样正常工作吗?

public static void runTask(String p)
{
     Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run()
            {
                pendingRequest = pendingRequest - 1;
                if (pendingRequest == 0)
                {
                        context.startActivity(p);
                }
            }
        }, 4000);
    }

}

What I want it to do is only run if pendingRequest is 0, but I have other activities that add to pending request after the runTask() is called. 我要执行的操作仅在未决请求为0时运行,但是在调用runTask()之后,我还有其他活动会添加到未决请求中。 If this doesn't make any sense please let me know and I will try to reword it. 如果这没有任何意义,请告诉我,我将尝试重新措词。

This is a bit of an obscure way to do things so seeing just this snippet I cant tell exactly what the desired behavior is, however, it should work if you make the parameter "p" final. 这是做事的一种晦涩方式,因此仅看到此代码段,我无法确切地说出所需的行为是什么,但是,如果将参数“ p”设置为final,它应该可以工作。 Im also not familiar with a startActivity method that takes a string instead of an intent, but I cant tell if "context" is actually an Android Context object, but I'm assuming it is. 我也不太熟悉采用字符串而不是意图的startActivity方法,但是我无法确定“上下文”是否实际上是Android Context对象,但我假设是。 What I'm not sure about is why you would wait 4 seconds BEFORE decrementing pendingRequest. 我不确定的是为什么您要在递减前将等待4秒钟。 I would think you want to decrement, allow 4 seconds for someone else to add a pending request, and if it's still 0 after the wait start the Activity... but, again, I cant tell from the snippet. 我想您想减少,让其他人有4秒的时间添加一个待处理的请求,并且在等待启动Activity之后它是否仍然为0 ...但是,我仍然无法从代码段中得知。

Try this: 尝试这个:

private static Object requestLock = new Object();
public static void runTask(final String p)
{
    synchronized(requestLock)
    {
        if (--pendingRequest > 0) // Decrement first
        {
            // There are more requests
            return;
        }
    }

    // Wait 4 sec and if there are still no requests start the activity.
     Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run()
            {
                synchronized(requestLock)
                {
                    if (pendingRequest == 0)
                    {
                        context.startActivity(p);
                    }
                }
            }
        }, 4000);
    }
}

Note: You will also need to add a synchronized block where you increment the pendingRequests. 注意:您还需要在增加未决请求的位置添加一个同步块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM