android - Check network connection in fragment -


i tried check network connection in sherlockfragment getsystemservice() method not recognized.

below code (from http://developer.android.com/training/basics/network-ops/connecting.html)

    connectivitymanager connmgr = (connectivitymanager) getsystemservice(context.connectivity_service);     networkinfo networkinfo = connmgr.getactivenetworkinfo();     if (networkinfo != null && networkinfo.isconnected()) {         // fetch data     } else {         // display error     } 

thanks in advance

the method getsystemservice() not defined on fragments, activity first using getactivity(), e.g.:

connectivitymanager connmgr = (connectivitymanager) getactivity()                              .getsystemservice(context.connectivity_service);  networkinfo networkinfo = connmgr.getactivenetworkinfo();  if (networkinfo != null && networkinfo.isconnected()) {     // fetch data } else {     // display error } 

p.s: additianal note: if there potential risk fragment running without being attached activity, check whether getactivity() returns null first.

cheers!


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -