java - Check if device has internet connection -


i want check if device has internet connection. have found lot of solutions can't in example:

if(device has internet connection){     webview.loadurl("http://the.url.com") } else{     toast.maketext(context, text, duration).show() } 

put method in class want check connectivity:

public static boolean isonline(context context) {     connectivitymanager cm = (connectivitymanager) context.getsystemservice(context.connectivity_service);     networkinfo netinfo = cm.getactivenetworkinfo();     if (netinfo != null && netinfo.isconnected()) {        return true;     }     return false; } 

then when need check connection, (using example):

if(isonline(getapplicationcontext()){     webview.loadurl("http://the.url.com") } else{     toast.maketext(context, text, duration).show() } 

you can create method in class , use there, exampleclass.isonline().

do not forget add androidmanifest:

<uses-permission android:name="android.permission.access_network_state" /> 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -