this project checks if active internet is present or not on device. Connecting to network doesn't mean internet access so this project pings google to check if internet is present or not
To use the library follow the following steps:
compile 'com.treebo:internetavailabilitychecker:$library_version'
onCreate()
function. This is necessary step before starting using the library because it needs context to register connectivity broadcast receiver.
It stores only weakreference to the context, so no need to worry about memory leaks.
Also it does lazy registration of receiver; i.e. it registers receiver whenever first listener attaches to listen to internet changes and unregister itself when last listener stops listening.
InternetAvailabilityChecker.init(this);
public class MainActivity extends AppCompatActivity implements InternetConnectivityListener {
@Override
public void onInternetConnectivityChanged(boolean isConnected) {
//do something based on connectivity
}
}
onCreate()
function).
It keeps a weakreference to the subscriber and clears the references whenever subscriber gets destroyed (when there is no more strong reference referencing it).
mInternetAvailabilityChecker = InternetAvailabilityChecker.getInstance();
mInternetAvailabilityChecker.addInternetConnectivityListener(this);
onDestroy()
, fragment’s onDetach()
or service’s onDestroy()
).
mInternetAvailabilityChecker.removeInternetConnectivityChangeListener(this);