Below is the code for checking internet via broadcast receiver.
public class CheckConnection extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
if(isOnline(context)){
//Do Stuff
}
else{
//Toast message saying "No internet"
}
}
public 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;
}
}
And I have say n events.For those n events I am calculating the distance between current location and destination using google distance matrix API.And also am using AsyncTask multiple times for calculating the distance.But now am confused on how to call broadcast receiver to check internet connection and call the method which calculates the distance.How to make sure whether the app is connected to internet or not all the time.
Thanks for your time.