标签:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
/** * 测试ConnectivityManager * ConnectivityManager主要管理和网络连接相关的操作 * 相关的TelephonyManager则管理和手机、运营商等的相关信息;WifiManager则管理和wifi相关的信息。 * 想访问网络状态,首先得添加权限<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> * NetworkInfo类包含了对wifi和mobile两种网络模式连接的详细描述,通过其getState()方法获取的State对象则代表着 * 连接成功与否等状态。 * */ public void testConnectivityManager() { ConnectivityManager connManager = (ConnectivityManager) this .getSystemService(CONNECTIVITY_SERVICE); // 获取代表联网状态的NetWorkInfo对象 NetworkInfo networkInfo = connManager.getActiveNetworkInfo(); // 获取当前的网络连接是否可用 boolean available = networkInfo.isAvailable(); if (available){ Log.i( "通知" , "当前的网络连接可用" ); } else { Log.i( "通知" , "当前的网络连接可用" ); } State state = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState(); if (State.CONNECTED==state){ Log.i( "通知" , "GPRS网络已连接" ); } state = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState(); if (State.CONNECTED==state){ Log.i( "通知" , "WIFI网络已连接" ); } // 跳转到无线网络设置界面 startActivity( new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS)); // 跳转到无限wifi网络设置界面 startActivity( new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS)); } |
android中判断网络连接是否可用
一、判断网络连接是否可用
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm == null) {
} else {
//如果仅仅是用来判断网络连接
//则可以使用 cm.getActiveNetworkInfo().isAvailable();
NetworkInfo[] info = cm.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
}
return false;
}
二、判断GPS是否打开
public static boolean isGpsEnabled(Context context) {
LocationManager lm = ((LocationManager) context
.getSystemService(Context.LOCATION_SERVICE));
List accessibleProviders = lm.getProviders(true);
return accessibleProviders != null && accessibleProviders.size() > 0;
}
三、判断WIFI是否打开
public static boolean isWifiEnabled(Context context) {
ConnectivityManager mgrConn = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
TelephonyManager mgrTel = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
return ((mgrConn.getActiveNetworkInfo() != null && mgrConn
.getActiveNetworkInfo().getState() == NetworkInfo.State.CONNECTED) || mgrTel
.getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS);
}
四、判断是否是3G网络
public static boolean is3rd(Context context) {
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkINfo = cm.getActiveNetworkInfo();
if (networkINfo != null
&& networkINfo.getType() == ConnectivityManager.TYPE_MOBILE) {
return true;
}
return false;
}
五、判断是wifi还是3g网络,用户的体现性在这里了,wifi就可以建议下载或者在线播放。
public static boolean isWifi(Context context) {
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkINfo = cm.getActiveNetworkInfo();
if (networkINfo != null
&& networkINfo.getType() == ConnectivityManager.TYPE_WIFI) {
return true;
}
return false;
}
android ConnectivityManager获取网络状态
标签:
原文地址:http://www.cnblogs.com/evertriely/p/4624790.html