android-验证网络是否可用
ConnectivityManager主要管理和网络连接相关的操作,NetworkInfo类包含了对wifi和mobile两种网络模式连接的详细描述,通过其getState()方法获取的State对象则代表着连接成功与否等状态。
CONNECTED
Detailed state | Coarse-grained state |
IDLE |
DISCONNECTED |
SCANNING |
CONNECTING |
CONNECTING |
CONNECTING |
AUTHENTICATING |
CONNECTING |
CONNECTED |
|
DISCONNECTING |
DISCONNECTING |
DISCONNECTED |
DISCONNECTED |
UNAVAILABLE |
DISCONNECTED |
FAILED |
DISCONNECTED |
if(State.CONNECTED==state){
Log.i("TAG", "GPRS网络已连接");
}
state = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
if(State.CONNECTED==state){
Log.i("TAG“, "WIFI网络已连接");
}
ConnectivityManager.TYPE_WIFI 表示wifi连接
ConnectivityManager.TYPE_MOBILE 表示3G连接
网络设置Intent:
new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS)
// 跳转到无线网络设置界面
new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS) // 跳转到无线wifi网络设置界面
最后别忘了在mainifest文件中加上权限:
"<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>"
posted on 2012-05-27 08:48 android开发实例 阅读(1301) 评论(0) 编辑 收藏 举报