You should check initial discussion on https://gist.github.com/PasanBhanu/730a32a9eeb180ec2950c172d54bb06a on.
Basically we were working a simple to use solution for checking network on android app, which can gracefully replace now deprecated NetworkInfo
classes, without disrupting codeflow for legacy apps. The problem is only when switching to wifi, not when switching to cellular.
Main working code part:
// Network Check
public void registerNetworkCallback()
{
try {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkRequest.Builder builder = new NetworkRequest.Builder();
connectivityManager.registerNetworkCallback(builder.build(),new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
Variables.isNetworkConnected = true; // Global Static Variable
}
@Override
public void onLost(Network network) {
Variables.isNetworkConnected = false; // Global Static Variable
}
}
);
Variables.isNetworkConnected = false;
}catch (Exception e){
Variables.isNetworkConnected = false;
}
}
The problem is when we switch to wifi, It for some reason makes a call to onLost after making call to onAvailable. Therefore setting value on static variable to false. Even when network is connected.
Here is a debug log on when network is switched to wifi:
2019-11-23 16:54:29.136 8416-8459/com.example.simplenetwork D/FLABS:: onAvailable
2019-11-23 16:54:29.136 8416-8459/com.example.simplenetwork D/FLABS:: onCapabilitiesChanged
2019-11-23 16:54:29.136 8416-8459/com.example.simplenetwork D/FLABS:: onLinkPropertiesChanged
2019-11-23 16:54:29.136 8416-8459/com.example.simplenetwork D/FLABS:: onBlockedStatusChanged
2019-11-23 16:54:29.307 8416-8459/com.example.simplenetwork D/FLABS:: onLosing
2019-11-23 16:54:29.325 8416-8459/com.example.simplenetwork D/FLABS:: onCapabilitiesChanged
2019-11-23 16:54:29.371 8416-8459/com.example.simplenetwork D/FLABS:: onLost
2019-11-23 16:54:29.959 8416-8459/com.example.simplenetwork D/FLABS:: onLinkPropertiesChanged
2019-11-23 16:54:29.975 8416-8459/com.example.simplenetwork D/FLABS:: onLinkPropertiesChanged
2019-11-23 16:54:30.972 8416-8459/com.example.simplenetwork D/FLABS:: onLinkPropertiesChanged
2019-11-23 16:54:31.693 8416-8459/com.example.simplenetwork D/FLABS:: onLinkPropertiesChanged
2019-11-23 16:54:32.053 8416-8459/com.example.simplenetwork D/FLABS:: onCapabilitiesChanged
I am trying adding additional checks in onCapabilitiesChanged()
but so far nothing is resolving things without breaking other thing.
You can check complete code(and related progress/discussion) on https://gist.github.com/PasanBhanu/730a32a9eeb180ec2950c172d54bb06a
My version of code( which contains complete debug process ) as a clean separate project just for testing this, is on https://gist.github.com/Abhinav1217/0ff6b39e70fa38379d61e85e09b49fe7 .
PS: Can someone suggest better title for this question, something which would be more logical?
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − To find the internet status we have to add network state permission to AndroidManifest. xml file as shown below.
In android, we can determine the internet connection status easily by using getActiveNetworkInfo() method of ConnectivityManager object. Following is the code snippet of using the ConnectivityManager class to know whether the internet connection is available or not.
Class that answers queries about the state of network connectivity. It also notifies applications when network connectivity changes.
tldr: Use registerDefaultNetworkCallback
instead of registerNetworkCallback
for simple connectivity solution.
I finally opened a ticket to Google Android bug tracker. They explained why registerNetworkCallback
works that way, For simple internet connectivity stuff They told me to use registerDefaultNetworkCallback
instead. There are several more options discussed on the original gist https://gist.github.com/PasanBhanu/730a32a9eeb180ec2950c172d54bb06a Thanks to the awesome community at github and stackoverflow.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With