广播接收器android的计时器

发布于 2024-11-09 11:46:54 字数 784 浏览 0 评论 0原文

我正在开发这个应用程序,我要扫描可到达的接入点。我必须每隔一秒就定期这样做。 我开始用普通的timerTask来做这件事,但效果不佳,因为它总是创建新线程。因此,我开始在 android 中使用处理程序类并调用 postDelayed 方法来安排扫描!就像这样:

protected void setTimer()
    {
        final long elapse = 100;
        Runnable t = new Runnable() {
            public void run()
            {

        Log.i(TAG3, "startedScan"); 
        IntentFilter filter = new    IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
        registerReceiver(wifiReceiver, filter);

        wifiManager.startScan();

                if( !isComplete )
                {
                    mHandler.postDelayed( this, elapse );
                }
            }
        };
        mHandler.postDelayed( t, elapse );
    }   

问题是扫描只运行 3 次,然后就再也不会运行了。我找不到解决方案!我该如何解决这个问题?

I'm developing this application were I do a scan for the reachable access points. I have to do it periodicaly only second after second.
I started to do it with a ordinary timerTask, but it didn't worked well because it is alaways creating new threads. So, I started using the handler class in android and calling a postDelayed method to schedule the scan!just like this:

protected void setTimer()
    {
        final long elapse = 100;
        Runnable t = new Runnable() {
            public void run()
            {

        Log.i(TAG3, "startedScan"); 
        IntentFilter filter = new    IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
        registerReceiver(wifiReceiver, filter);

        wifiManager.startScan();

                if( !isComplete )
                {
                    mHandler.postDelayed( this, elapse );
                }
            }
        };
        mHandler.postDelayed( t, elapse );
    }   

The problem is that the scan is only running 3 times and then it never runns again..I can't find a solution!How can I solve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

才能让你更想念 2024-11-16 11:46:54

我猜测 isComplete 被设置为 true,因此 Runnable 不会被重新安排。我建议将 Runnable 移出该方法,然后将重新安排添加到 wifiReceiver 的 onReceive 方法中。

Runnable t = new Runnable() {
    public void run()
    {
        Log.i(TAG3, "startedScan"); 
        IntentFilter filter = new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
        registerReceiver(wifiReceiver, filter);
        wifiManager.startScan();
    }
};

protected void setTimer()
{
    final long elapse = 100;
    mHandler.postDelayed( t, elapse );
}   

I'm guessing isComplete is getting set to true, so the Runnable isn't being re-scheduled. I'd suggest moving the Runnable out of the method, and then adding the reschedule to wifiReceivers onReceive method.

Runnable t = new Runnable() {
    public void run()
    {
        Log.i(TAG3, "startedScan"); 
        IntentFilter filter = new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
        registerReceiver(wifiReceiver, filter);
        wifiManager.startScan();
    }
};

protected void setTimer()
{
    final long elapse = 100;
    mHandler.postDelayed( t, elapse );
}   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文