<span style="font-size:14px;">/**</span>
<span style="font-size:14px;"> * 判断是否有网络链接
* @param mActivity
* @return false 或者是true
*/
public static boolean isNetworkAvailable(Context context){
//获取系统的连接服务
ConnectivityManager connectivity=(ConnectivityManager)
context.getSystemService(context.CONNECTIVITY_SERVICE);
if(connectivity ==null){
return false;
}else{
//NetworkInfo类包含了对wifi和mobile两种网络模式连接的详细描述,通过其getState()方法获取的State对象则代表着 连接成功与否等状态。
NetworkInfo info =connectivity.getActiveNetworkInfo();
if( info !=null){
return info.isAvailable();
}
}
return false;
}
/**
* 判断WiFi网络有用
* @param context
* @return
*/
public boolean IsWIFIConnected (Context context){
if(context !=null){
ConnectivityManager manager=(ConnectivityManager) context.getSystemService(context.CONNECTIVITY_SERVICE);
NetworkInfo info=manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if(info !=null){
return info.isAvailable();
}
}
return false;
}
/**
* 判断mobile网络是否可用
* @param context
* @return
*/
public boolean IsMoblieConnected(Context context){
if(context !=null){
ConnectivityManager manager=(ConnectivityManager) context.getSystemService(context.CONNECTIVITY_SERVICE);
NetworkInfo info=manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if(info !=null){
return info.isAvailable();
}
}
return false;
}
/**
* 获取网络链接的类型
* @param context
* @return i 和类型的名字
*/
public int ismobileconnecteds(Context context){
if(context !=null){
ConnectivityManager manager=(ConnectivityManager) context.getSystemService(context.CONNECTIVITY_SERVICE);
NetworkInfo info=manager.getActiveNetworkInfo();
if(info !=null && info.isAvailable()){
int i=info.getType();
String name=info.getTypeName();
Log.e("jun", name+"ss"+i);
return i;
}
}
return -1;
}</span>
<span style="background-color: rgb(204, 204, 204);"><span style="color:#ff0000;">
</span></span>
<span style="background-color: rgb(204, 204, 204);"><span style="font-size:32px;color:#ff0000;"><strong>注意:一定有加上权限</strong></span></span>
<span style="background-color: rgb(204, 204, 204);"><span style="font-size:32px;color:#ff0000;"><strong><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/></strong></span></span>