开发者

how to get device network information? (Android)

the application i'm currently working on is开发者_运维百科 mobile network dependant, so my question is, is it possible to get the current mobile network which being used on the device (e.g. 3 UK, T-Mobile)?

also, is there anyway of getting the user's mobile number?

thanks for any help (:


Phone Number:

        final TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
        final String phoneNumber = tm.getLine1Number();

Network Type:

        // Check each connection type
        boolean connectionAvailable = false;
        ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);

        /**
         * WIFI
         */

        /** Check the connection **/
        NetworkInfo network = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        // Make sure the network is available
        if(network != null && network.isAvailable() && network.isConnectedOrConnecting()) {
            connectionAvailable = true;
        }

        /**
         * 2G/3G
         */            
        /** Check the connection **/
        network = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

        // Show the right icon
        if(network != null &&
                (network.getSubtype() == TelephonyManager.NETWORK_TYPE_GPRS ||
                 network.getSubtype() == TelephonyManager.NETWORK_TYPE_EDGE)) {
            // 2G
        }
        else {
            // 3G
        }

        // Make sure the network is available
        if(network.isAvailable() && network.isConnectedOrConnecting()) {
            connectionAvailable = true;
        }

        /**
         * 4G
         */

        /** Check the connection **/
        network = cm.getNetworkInfo(ConnectivityManager.TYPE_WIMAX);

        // Make sure the network is available
        if(network != null && network.isAvailable() && network.isConnectedOrConnecting()) {
            connectionAvailable = true;
        }


Everything you are looking for is in the TelephonyManager. Example usage:

final TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
final String phoneNumber = tm.getLine1Number();


if (this.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY)) {
    TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    String msisdn = telephonyManager.getLine1Number();
    String carrier = telephonyManager.getNetworkOperatorName();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜