网络可用性检查返回 false 但手机仍处于在线状态

发布于 2024-09-10 20:06:42 字数 1100 浏览 5 评论 0原文

在检查网络是否可用时,我在获得一致结果方面遇到一些问题。

我在 AppPreferences 类中使用此代码片段来检查网络的可用性。

/**
     * @return the networkAvailable
     */
    public boolean isNetworkAvailable() {
        connectionManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        networkAvailable = connectionManager.getActiveNetworkInfo() != null && connectionManager.getActiveNetworkInfo().isConnected();
        return networkAvailable;
    }

在每次运行之前,我将上下文设置如下:

timer.scheduleAtFixedRate(

                new TimerTask() {

                    public void run() {

                        appPreferences.setContext(getBaseContext());

                        if (appPreferences.isNetworkAvailable()){

                            // perform task

                        }

                    }
                },
                0,
                UPDATE_INTERVAL);

我确实知道它没有绑定到后台线程,因为我有一个 onReceive 调用执行相同的逻辑,但此检查仍然失败。

当它在蜂窝数据连接和 WiFi 之间移动时(反之亦然),似乎主要会发生这种情况。即使我更新它,它启动的上下文似乎仍然存在。

有谁知道这里可能有什么问题吗?

I have am having some issues with getting consistent results when checking if the network is available or not.

I use this code snippet inside a class AppPreferences to check the availability of a network.

/**
     * @return the networkAvailable
     */
    public boolean isNetworkAvailable() {
        connectionManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        networkAvailable = connectionManager.getActiveNetworkInfo() != null && connectionManager.getActiveNetworkInfo().isConnected();
        return networkAvailable;
    }

Before each run I set the context as below:

timer.scheduleAtFixedRate(

                new TimerTask() {

                    public void run() {

                        appPreferences.setContext(getBaseContext());

                        if (appPreferences.isNetworkAvailable()){

                            // perform task

                        }

                    }
                },
                0,
                UPDATE_INTERVAL);

I do know it is not tied to the background thread as I have a onReceive call doing the same logic and still this check fails.

It seems to predominantly happen when it moves between a cellular data connection and to wifi, or vice versa. The Context in which it was started seems to stay even though I update it.

Does anyone have any idea what could be the issue here?

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

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

发布评论

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

评论(1

风向决定发型 2024-09-17 20:06:42

似乎活动网络信息将保留在服务/活动/接收器的上下文启动时的状态。因此,如果您在网络上启动它,然后断开连接(即从 3G 移动到 Wifi 并断开 3G 连接),它将保留在第一个活动连接上,使应用程序认为手机已离线,即使事实并非如此。

在我看来,最好的解决方案是使用 getApplicationContext,因为这不会与您启动特定“任务”的时间相关。

更新:相关的是,如果您在连接 Wifi 的情况下在 Android(特别是 Nexus One)上运行应用程序很长一段时间,请检查并确保在屏幕亮起时不要让 Wifi 休眠。睡觉了。您可以在“无线网络”下的“高级”选项中进行设置。

It seems as if the active network info will stay on the state of when the Context of the Service/Activity/Receiver is started. Hence if you start it on a network, and then later disconnect from that (i.e. moves from 3G to Wifi and disconnect the 3G connection) it will stay on the first active connection making the app believe the phone is offline even though it is not.

It seems to me that the best solution is to user getApplicationContext instead as that will not be tied to when you started the particular "task".

Update: Related is that if you run applications on Androids (in particular Nexus One) for a long period of time when connected to Wifi do check that you make sure you do not let the Wifi sleep when the screen sleeps. You will be able to set that at the Advanced option under Wireless Networks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文