What is the correct way of checking for mobile network available (no data connection)

后端 未结 6 890
名媛妹妹
名媛妹妹 2021-02-02 15:35

What is the correct way of checking if a mobile network (GSM) connection is available on Android? (>2.1) I don\'t want to check if there is data connection available over the mo

6条回答
  •  南旧
    南旧 (楼主)
    2021-02-02 16:16

    I used the following code to know if I'm connected to the network (no mobile data):

        public static Boolean isMobileAvailable(Context appcontext) {       
        TelephonyManager tel = (TelephonyManager) appcontext.getSystemService(Context.TELEPHONY_SERVICE);       
        return ((tel.getNetworkOperator() != null && tel.getNetworkOperator().equals("")) ? false : true);      
    }
    

    The NetworkOperator is returned only if the phone is actually registered to a network.

提交回复
热议问题