API文档内容:
NetworkInfo android.net.
ConnectivityManager.getActiveNetworkInfo()
Returns details about the currently active default data network. When connected, this network is the default route for outgoing connections. You should always checkNetworkInfo.isConnected()
before initiating network traffic. This may returnnull
when there is no default network.
-
Returns:
-
a
NetworkInfo
object for the current default network ornull
if no network default network is currently activeThis method requires the call to hold the permission
android.Manifest.permission.ACCESS_NETWORK_STATE
. -
实际执行的时候却报错
-
08-26 09:14:44.896: E/AndroidRuntime(12009): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testnetwork/com.example.testnetwork.MainActivity}: java.lang.SecurityException: ConnectivityService: Neither user 10102 nor current process has android.permission.ACCESS_NETWORK_STATE. 08-26 09:16:47.651: E/AndroidRuntime(12276): Caused by: java.lang.SecurityException: ConnectivityService: Neither user 10102 nor current process has android.permission.ACCESS_NETWORK_STATE.
-
报错提示缺少权限android.permission.ACCESS_NETWORK_STATE.
-
添加之后正常,但是有个小细节,添加权限的时候不能在代码里面添加,应该是个bug,我在代码里面添加之后任然报这个错,即使clean了工程重启eclipse之后还是一样的错误。后来在图形界面添加之后正常