Skip to content

Instantly share code, notes, and snippets.

@sagorbrur
Created November 22, 2017 15:31
Show Gist options
  • Save sagorbrur/98fe69632c6908179f67745c7af425d9 to your computer and use it in GitHub Desktop.
Save sagorbrur/98fe69632c6908179f67745c7af425d9 to your computer and use it in GitHub Desktop.
import android.app.Service;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
/**
* Created by Sagor Sarker on 17-Nov-17.
*/
public class ConnectionDetector {
Context context;
public ConnectionDetector(Context context) {
this.context = context;
}
public boolean isConnected()
{
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Service.CONNECTIVITY_SERVICE);
if(cm!=null){
NetworkInfo info = cm.getActiveNetworkInfo();
if(info!=null){
if(info.getState() == NetworkInfo.State.CONNECTED)
return true;
}
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment