关闭

Android获取网络状态

306人阅读 评论(0) 收藏 举报
分类:

获取权限

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
    <uses-permission android:name="android.permission.INTERNET"/>


GetNetworkState.java

package com.klnnxb.cmge;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;


public class GetNetworkState {

	 public static int getAPNType(Context context){ 

	        int netType = -1;  

	        ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 

	        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); 

	         

	        if(networkInfo==null){ 

	            return netType; 

	        } 

	        int nType = networkInfo.getType(); 

	        if(nType==ConnectivityManager.TYPE_MOBILE){ 

	            Log.e("networkInfo.getExtraInfo()", "networkInfo.getExtraInfo() is "+networkInfo.getExtraInfo()); 

	            if(networkInfo.getExtraInfo().toLowerCase().equals("cmnet")){ 

	                netType = 1; //CMNET; 

	            } 

	            else{ 

	                netType = 2; //CMWAP; 

	            } 

	        } 

	        else if(nType==ConnectivityManager.TYPE_WIFI){ 

	            netType = 3; //WIFI; 

	        } 

	        return netType; 

	    } 

}

使用:

private static GetNetworkState mGetNetworkState;
int netType = mGetNetworkState.getAPNType(context);


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:92698次
    • 积分:2354
    • 等级:
    • 排名:第16440名
    • 原创:142篇
    • 转载:36篇
    • 译文:0篇
    • 评论:6条
    最新评论