开发者

Network connection lost every time an Activity is created?

开发者 https://www.devze.com 2023-01-15 19:17 出处:网络
I am checking to see if my app has a network connection: public boolean isOnline(){ ConnectivityManager conMgr =(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);

I am checking to see if my app has a network connection:

public boolean isOnline(){
        ConnectivityManager conMgr =  (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);

        if ( conMgr.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED 
            ||  conMgr.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTING  ) {

            return true;

        }
        else if ( conMgr.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED 
            ||  conMgr.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED) {
            return fal开发者_如何学Cse;

        } else {return false;}
    }

Whenever I rotate my screen between landscape and portrait this method returns false. It makes me wonder if Network connections are getting killed during the rotation?


No... your network connections don't get killed during rotation. Probably, what is going on is that you are not saving the boolean value of the connection state, so when the handset is rotated that value gets to its default (false). Check this thread to know how to save the state: How do I save an Android application's state?

0

精彩评论

暂无评论...
验证码 换一张
取 消