您的位置:首页 > 理论基础 > 计算机网络

安卓下测试网络连接是否可用的问题

2014-08-26 09:54 399 查看


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 check
NetworkInfo.isConnected()
before
initiating network traffic. This may return
null
when there is no default network.

Returns:a
NetworkInfo
object for the current default network or
null
if no network default network is currently active
This method requires the call to hold the permission
android.Manifest.permission.ACCESS_NETWORK_STATE
.

API里提到的权限是
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之后还是一样的错误。后来在图形界面添加之后正常
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: