简体   繁体   English

“State.CONNECTED”:CONNECTED无法解析或不是字段

[英]“State.CONNECTED”: CONNECTED cannot be resolved or is not a field

I want to check if my Android app is connected to the Internet. 我想检查一下我的Android应用是否已连接到互联网。 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: 我还给了我的应用程序访问网络状态的权限,但是Eclipse在State.CONNECTED旁边说了这个:

CONNECTED cannot be resolved or is not a field. CONNECTED无法解析或不是字段。

Even books are wrong, now? 现在连书都错了吗? x( Thanks in advance. x(提前致谢。

you have imported the wrong state change 您导入了错误的状态更改

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

to

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM