Last active
November 18, 2020 17:15
-
-
Save ExNDY/2c27b9c29b9642accbb0cd4f33ca9421 to your computer and use it in GitHub Desktop.
Monitoring network connection for Android ( API 24 ) with using LiveData
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Activity extends AppCompatActivity{ | |
... | |
private NetworkConnectionMonitor connectionMonitor; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity); | |
... | |
connectionMonitor = new NetworkConnectionMonitor(getApplicationContext()); | |
connectionMonitor.observe(this, new Observer<Boolean>() { | |
@Override | |
public void onChanged(Boolean isConnected) { | |
if (isConnected){ | |
//DO void Online(); | |
} | |
else { | |
//DO void Offline(); | |
} | |
} | |
}); | |
... | |
} | |
@Override | |
protected void onPause(){ | |
if (Variables.isRegistered){ | |
connectionMonitor.unregisterDefaultNetworkCallback(); | |
Variables.isRegistered=false; | |
} | |
super.onPause(); | |
} | |
@Override | |
protected void onResume(){ | |
super.onResume(); | |
connectionMonitor.registerDefaultNetworkCallback(); | |
Variables.isRegistered=true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.net.ConnectivityManager; | |
import android.net.LinkProperties; | |
import android.net.Network; | |
import android.net.NetworkCapabilities; | |
import android.net.NetworkInfo; | |
import android.util.Log; | |
import androidx.annotation.NonNull; | |
import androidx.lifecycle.LiveData; | |
public class NetworkConnectionMonitor extends LiveData<Boolean> { | |
Context context; | |
ConnectionNetworkCallback callback; | |
public NetworkConnectionMonitor(Context context) { | |
this.context = context; | |
} | |
public void unregisterDefaultNetworkCallback(){ | |
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); | |
assert connectivityManager != null; | |
connectivityManager.unregisterNetworkCallback(callback); | |
} | |
public void registerDefaultNetworkCallback() { | |
try { | |
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); | |
postValue(checkConnection(connectivityManager)); | |
assert connectivityManager != null; | |
callback = new ConnectionNetworkCallback(); | |
connectivityManager.registerDefaultNetworkCallback(callback); | |
} catch (Exception e) { | |
Log.d("Connection: Exception in registerDefaultNetworkCallback", "xD"); | |
postValue(false); | |
} | |
} | |
private boolean checkConnection(@NonNull ConnectivityManager connectivityManager){ | |
Network network = connectivityManager.getActiveNetwork(); | |
if (network == null){ | |
return false; | |
} | |
else{ | |
NetworkCapabilities actNw = connectivityManager.getNetworkCapabilities(network); | |
return actNw != null | |
&& (actNw.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) | |
|| actNw.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) | |
|| actNw.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)); | |
} | |
} | |
private class ConnectionNetworkCallback extends ConnectivityManager.NetworkCallback{ | |
@Override | |
public void onAvailable(@NonNull android.net.Network network) { | |
super.onAvailable(network); | |
postValue(true); | |
Log.d("Connection:", "onAvailable"); | |
} | |
@Override | |
public void onLost(@NonNull android.net.Network network) { | |
super.onLost(network); | |
postValue(false); | |
Log.d("Connection:", "onLost"); | |
} | |
@Override | |
public void onBlockedStatusChanged(@NonNull Network network, boolean blocked) { | |
super.onBlockedStatusChanged(network, blocked); | |
Log.d("Connection:", "onBlockedStatusChanged"); | |
} | |
@Override | |
public void onCapabilitiesChanged(@NonNull Network network, @NonNull NetworkCapabilities networkCapabilities) { | |
super.onCapabilitiesChanged(network, networkCapabilities); | |
Log.d("Connection:", "onCapabilitiesChanged"); | |
} | |
@Override | |
public void onLinkPropertiesChanged(@NonNull Network network, @NonNull LinkProperties linkProperties) { | |
super.onLinkPropertiesChanged(network, linkProperties); | |
Log.d("Connection:", "onLinkPropertiesChanged"); | |
} | |
@Override | |
public void onLosing(@NonNull Network network, int maxMsToLive) { | |
super.onLosing(network, maxMsToLive); | |
Log.d("Connection:", "onLosing"); | |
} | |
@Override | |
public void onUnavailable() { | |
super.onUnavailable(); | |
Log.d("Connection:", "onUnavailable"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Variables { | |
//Store status of registration Networkcallback | |
public static boolean isRegistered = false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment