How to check for phone network on Android devices

时间秒杀一切 提交于 2019-12-11 04:18:51

问题


I know how to check if I have internet access (using the code from this post),but is it possible to check if a phone has telephone network access? For example someone might have access to the internet via Wifi but not have phone network access to send SMS or make calls.

In my case, while using a real device (Samsung Galaxy S), I am able to turn of my 3G network (then the phone will detect I am not connected to the internet), but I am still able to make phone calls and send SMS. I guess I must be using some other network...

How do I test whether the phone network is connected? Do I need the TelephonyManager?

Thankyou for your time.

Mel


回答1:


Sure would you not just use this: getNetworkType()

boolean hasNetwork = android.telephony.TelephonyManager.getNetworkType() != android.telephony.TelephonyManager.NETWORK_TYPE_UNKNOWN; 
// True if the phone is connected to some type of network i.e. has signal



回答2:


The above answer did not work for one of my app users - he was connected to the Network but his NetworkType was unknown. Screenshot: http://dl.dropbox.com/u/5072192/SC20130317-161413.png

I am instead checking for the NetworkOperator field. (From the first answer here What is the correct way of checking for mobile network available (no data connection))

public static boolean isMobileAvailable(Context acontext) {       
    TelephonyManager tel = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);       
    return (tel.getNetworkOperator() != null && !tel.getNetworkOperator().equals(""));      
}


来源:https://stackoverflow.com/questions/5529484/how-to-check-for-phone-network-on-android-devices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!