If you want find or check you have to connected to internet or not. It is simple to code of your application like this:
First you have to create custom class, ConnectionDetector.java
Now call to the mainactivity java class,
Happy Coding !!
First you have to create custom class, ConnectionDetector.java
public class ConnectionDetector {
private Context _context;
public ConnectionDetector(Context context){
this._context = context;
}
public boolean isOnline(){
ConnectivityManager cm = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if (ni != null && ni.isConnected())
{
return true;
}
return false;
}
}
Now call to the mainactivity java class,
ConnectionDetector cd = new ConnectionDetector(getApplicationContext());
Boolean isInternetPresent = cd.isOnline();
if (isInternetPresent) {
//Do Something if internet connection
}
Happy Coding !!
0 comments
Posts a comment