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
State state = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
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"/>"
更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11