簡體   English   中英

如何獲取設備網絡信息? (Android)

[英]how to get device network information? (Android)

我當前正在處理的應用程序依賴於移動網絡,所以我的問題是,能否獲得設備上正在使用的當前移動網絡(例如3 UK,T-Mobile)?

另外,是否有獲取用戶手機號碼的方法?

謝謝你的幫助 (:

電話號碼:

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

您要查找的所有內容都在TelephonyManager 用法示例:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM