如何获取设备网络信息? (机器人)

时间:2011-08-05 12:55:25

标签: java android android-manifest

我目前正在开发的应用程序依赖于移动网络, 所以我的问题是,是否有可能获得当前在设备上使用的移动网络(例如3 UK,T-Mobile)?

另外,有没有得到用户的手机号码?

感谢您的帮助(:

3 个答案:

答案 0 :(得分:10)

电话号码:

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

网络类型:

        // 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;
        }

答案 1 :(得分:1)

您正在寻找的所有内容都在TelephonyManager中。用法示例:

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

答案 2 :(得分:0)

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