user1810448
user1810448

Reputation: 13

"State.CONNECTED": CONNECTED cannot be resolved or is not a field

I want to check if my Android app is connected to the Internet. I copied the code I read in a book, here it is:

ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
                    NetworkInfo networkInfo = cm.getActiveNetworkInfo();
                    int networkType = networkInfo.getType();
                    android.net.NetworkInfo.State networkState = networkInfo.getState();
                    if (networkState.compareTo(State.CONNECTED)==0)
                    {
                        //We are connected!!!
                    }

I've also given my app the permission to access network state, but Eclipse says this next to State.CONNECTED:

CONNECTED cannot be resolved or is not a field.

Even books are wrong, now? x( Thanks in advance.

Upvotes: 0

Views: 997

Answers (1)

sherif
sherif

Reputation: 2332

you have imported the wrong state change

 if (networkState.compareTo(State.CONNECTED)==0)

to

 if (networkState.compareTo(android.net.NetworkInfo.State.CONNECTED)==0)

Upvotes: 2

Related Questions