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:
- Add the dependency as follows:
compile 'com.treebo:internetavailabilitychecker:$library_version'
- Initialise it in application’s
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);
- Implement InternetConnectivityListener interface where ever you want to listen to internet connectivity changes (E.g. in activity, fragment or service).
public class MainActivity extends AppCompatActivity implements InternetConnectivityListener { @Override public void onInternetConnectivityChanged(boolean isConnected) { //do something based on connectivity } }
- Get instance of InternetAvailabilityChecker and Add listener whenever you want to start listening to connectivity changes (e.g. in activity’s
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);
- Remove listener whenever you are done (e.g. in activity’s
onDestroy()
, fragment’sonDetach()
or service’sonDestroy()
).mInternetAvailabilityChecker.removeInternetConnectivityChangeListener(this);