Android获取网络状态

获取权限

<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);


你可能感兴趣的:(Android获取网络状态)