I found a lot of topics how to check the Internet access. But I cannot understand how to make it dynamically. For example, I send a request (it will work in a some another thread) and device loses the Internet connection. How I can cancel my request on this event?
Asked
Active
Viewed 3,613 times
2
-
1You can use a background service to continuously monitor your internet connection. It might has some effect. If you are making a network request, you can as well set a time-out once there is not response after that time, the request will cancel. – Inducesmile Mar 03 '16 at 01:16
-
Just I think if I set the short timeout when user with a bad connection speed can has error although device will have the Internet access. Or am I mistake? – Denis Sologub Mar 03 '16 at 01:19
-
1Then you have to check how much time different connection speed will to get respond, you can now have a benchmark. I think it might a better that using a background service running continuously. – Inducesmile Mar 03 '16 at 01:25
-
Thanks for answer! I'll write service then. – Denis Sologub Mar 03 '16 at 01:28
3 Answers
5
Here's something I cooked earlier; maybe it will help:
public class NetworkStateMonitor extends BroadcastReceiver {
Context mContext;
boolean mIsUp;
public interface Listener {
void onNetworkStateChange(boolean up);
}
public NetworkStateMonitor(Context context) {
mContext = context;
//mListener = (Listener)context;
IntentFilter intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
context.registerReceiver(this, intentFilter);
mIsUp = isUp();
}
/**
* call this when finished with it, and no later than onStop(): callback will crash if app has been destroyed
*/
public void unregister() {
mContext.unregisterReceiver(this);
}
/*
* can be called at any time
*/
public boolean isUp() {
ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo!=null && activeNetworkInfo.isConnectedOrConnecting();
}
/**
* registerReceiver callback, passed to mListener
*/
@Override public void onReceive(Context context, Intent intent) {
boolean upNow = isUp();
if (upNow == mIsUp) return; // no change
mIsUp = upNow;
((Listener)mContext).onNetworkStateChange(mIsUp);
}
}

c-an
- 3,543
- 5
- 35
- 82

Peter McLennan
- 371
- 4
- 11
-
-
Can you give me the way how I can apply this in detail? I wanna show custom dialog when the internet connection is disabled at any time. Not just when the status changes. But when it's not connected even though there is no previous connection. – c-an Jun 20 '19 at 07:10
-
https://www.tutorialspoint.com/how-to-check-internet-connection-in-android – A.Najafi Aug 15 '19 at 18:57
1
this my suggest, call this method in your function to get know that internet available or not
private boolean isNetworkConnected() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo() != null;
}
in your manifests
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
or you can put the method in your parent activity or fragment (conditional).

R Besar
- 574
- 1
- 5
- 12
-1
you can use connectivity manager
ConnectivityManager cm =
(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();

Konstantin Volkov
- 300
- 1
- 9