android - getTypeName() of NetworkInfo returns mobile eventhough connected to WIFI on mobile device -
this question has answer here:
- how compare strings in java? 23 answers
in following code, activeinfo.gettypename() returns mobile when connected wifi. read somewhere wifi not detected on emulator tried on device. still returns either mobile - when connected wifi or mobile data (i tried turning off mobile data , using wifi instead of turning on both)- , "not connected" when turn off both wifi , mobile data.
connectivitymanager conmgr = (connectivitymanager)getsystemservice(context.connectivity_service); networkinfo activeinfo = conmgr.getactivenetworkinfo(); wifimanager wifi=(wifimanager) getsystemservice(context.wifi_service); log.e("sometag", activeinfo.gettypename()); string ssid; if(activeinfo!=null && activeinfo.isconnected()) { //eventhough following comparison wrong, next else should return "wifi" right? well, doesn't. if(activeinfo.gettypename()=="wifi") { wifiinfo w=wifi.getconnectioninfo(); ssid= w.getssid(); } else { ssid = activeinfo.gettypename(); } } else ssid= "not connected";
the problem on line:
if (activeinfo.gettypename() == "wifi") {
unlike other programming languages, using ==
operator strings in java not produce expected result. that's because tests reference
equality (i.e. same position in memory) instead of equality of content.
please take @ how compare strings in java?
Comments
Post a Comment