Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
362 views
in Technique[技术] by (71.8m points)

android - Get Network type

I've been trying to retrive the current network type, but no success

when i say network type: i refer to know this info: if the type is: NETWORK_TYPE_IDEN or NETWORK_TYPE_UMTS.. and so on..

i tried to use:

NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();

or

NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo 
            (ConnectivityManager.TYPE_MOBILE); 

but no success..

i am doing this coz i wanna know if the current network is IDEN, or if the current network is connected through wifi..

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I hate magic numbers :

/**
 * You need to add:
 * 
 * <pre>
 *     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 * </pre>
 * 
 * in your AndroidManifest.xml.
 */
private String networkType() {
    TelephonyManager teleMan = (TelephonyManager)
            getSystemService(Context.TELEPHONY_SERVICE);
    int networkType = teleMan.getNetworkType();
    switch (networkType) {
        case TelephonyManager.NETWORK_TYPE_1xRTT: return "1xRTT";
        case TelephonyManager.NETWORK_TYPE_CDMA: return "CDMA";
        case TelephonyManager.NETWORK_TYPE_EDGE: return "EDGE";
        case TelephonyManager.NETWORK_TYPE_EHRPD: return "eHRPD";
        case TelephonyManager.NETWORK_TYPE_EVDO_0: return "EVDO rev. 0";
        case TelephonyManager.NETWORK_TYPE_EVDO_A: return "EVDO rev. A";
        case TelephonyManager.NETWORK_TYPE_EVDO_B: return "EVDO rev. B";
        case TelephonyManager.NETWORK_TYPE_GPRS: return "GPRS";
        case TelephonyManager.NETWORK_TYPE_HSDPA: return "HSDPA";
        case TelephonyManager.NETWORK_TYPE_HSPA: return "HSPA";
        case TelephonyManager.NETWORK_TYPE_HSPAP: return "HSPA+";
        case TelephonyManager.NETWORK_TYPE_HSUPA: return "HSUPA";
        case TelephonyManager.NETWORK_TYPE_IDEN: return "iDen";
        case TelephonyManager.NETWORK_TYPE_LTE: return "LTE";
        case TelephonyManager.NETWORK_TYPE_UMTS: return "UMTS";
        case TelephonyManager.NETWORK_TYPE_UNKNOWN: return "Unknown";
    }
    throw new RuntimeException("New type of network");
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...