android - getTypeName() of NetworkInfo returns mobile eventhough connected to WIFI on mobile device -


this question has answer here:

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

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -