码迷,mamicode.com
首页 > 其他好文 > 详细

检查网络是否可用

时间:2015-06-26 10:34:21      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:

/**
     * 检测网络是否可用1
     * @param act
     * @return
     */
    public boolean isNetConnected(Activity act){
            
            ConnectivityManager manager = (ConnectivityManager) act  
                   .getApplicationContext().getSystemService(  
                          Context.CONNECTIVITY_SERVICE);  
             
            if (manager == null) {  
                return false;  
            }  
            NetworkInfo networkinfo = manager.getActiveNetworkInfo();  
            if (networkinfo == null || !networkinfo.isAvailable()) {  
                return false;  
            }  
            return true;  
    }

/**
     * 检測网络是否可用2
     */
private boolean isNetConnected() {
        PppoeManager pppoeManager = PppoeManager.getInstance();
        EthernetManager ethernetManager = EthernetManager.getInstance();
        ConnectivityManager connectivityManager = (ConnectivityManager)
	 getSystemService(CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getNetworkInfo
	(ConnectivityManager.TYPE_WIFI);
        if (networkInfo.isConnected() || ethernetManager.isNetworkConnected()
                || pppoeManager.getPppoeStatus().equals
		(PppoeManager.PPPOE_STATE_CONNECT)) {
            return true;
        } else {
            return false;
        }
    }

//增加网络权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

检查网络是否可用

标签:

原文地址:http://www.cnblogs.com/mengfanrong/p/4601503.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!