NetworkAvailability检查返回false却把手​​机在线 [英] NetworkAvailability check returns false yet the phone is online

查看:102
本文介绍了NetworkAvailability检查返回false却把手​​机在线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经有我与得到检查时,如果网络是否可用一致的结果的一些问题。

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

我用里面的一类应用preferences这个code段来检查网络的可用性。

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);

我不知道它是不依赖于后台线程,因为我有一个电话的onReceive做同样的逻辑,仍然检查失败。

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.

这似乎$ P $其移动蜂窝数据之间的连接和WLAN网络时,反之亦然pdominantly发生。在其被启动的背景下似乎停留,即使我更新它。

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?

推荐答案

看起来好像活动的网络信息会留在当服务/活动的背景/接收器启动的状态。因此,如果你开始就在网络上,然后再从断开(即从3G移动到WIFI和断开3G连接),它会停留在第一个活动连接使得应用程序认为手机是离线,即使它不是。

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.

在我看来,最好的解决办法是用户,而不是getApplicationContext作为不会被捆绑,当你启动了特殊的任务。

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".

更新:相关的是,如果你在连接到无线运行机器人程序(尤其是Nexus One的)为很长一段时间做检查,你要确保你不要让无线睡眠当屏幕睡觉。您将能够设定,在无线网络下的高级选项。

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.

这篇关于NetworkAvailability检查返回false却把手​​机在线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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