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
Post a Comment