判断是否有网络链接

4 篇文章 0 订阅
1 篇文章 0 订阅
<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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值