Skip to content

Instantly share code, notes, and snippets.

@ExNDY
Last active November 18, 2020 17:15
Show Gist options
  • Save ExNDY/2c27b9c29b9642accbb0cd4f33ca9421 to your computer and use it in GitHub Desktop.
Save ExNDY/2c27b9c29b9642accbb0cd4f33ca9421 to your computer and use it in GitHub Desktop.

Revisions

  1. ExNDY revised this gist Oct 24, 2020. 1 changed file with 29 additions and 15 deletions.
    44 changes: 29 additions & 15 deletions NetworkConnectionMonitor.java
    Original file line number Diff line number Diff line change
    @@ -3,6 3,7 @@
    import android.net.LinkProperties;
    import android.net.Network;
    import android.net.NetworkCapabilities;
    import android.net.NetworkInfo;
    import android.util.Log;

    import androidx.annotation.NonNull;
    @@ -14,41 15,54 @@ public class NetworkConnectionMonitor extends LiveData<Boolean> {

    public NetworkConnectionMonitor(Context context) {
    this.context = context;
    }
    //Network status monitoring
    }
    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);
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    postValue(checkConnection(connectivityManager));
    assert connectivityManager != null;
    callback = new ConnectionNetworkCallback();
    connectivityManager.registerDefaultNetworkCallback(callback);
    postValue(false);
    } catch (Exception e) {
    Log.d("Connection: Exception in registerDefaultNetworkCallback", "xD");
    postValue(false);
    }
    }
    //Destroy
    public void unregisterDefaultNetworkCallback(){
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    assert connectivityManager != null;
    connectivityManager.unregisterNetworkCallback(callback);
    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));
    }
    }
    //Custom NetworkCallback

    private class ConnectionNetworkCallback extends ConnectivityManager.NetworkCallback{

    @Override
    public void onAvailable(@NonNull android.net.Network network) {
    super.onAvailable(network);
    postValue(true);
    postValue(true);
    Log.d("Connection:", "onAvailable");
    }

    @Override
    @Override
    public void onLost(@NonNull android.net.Network network) {
    super.onLost(network);
    postValue(false);
    Log.d("Connection:", "onLost");
    super.onLost(network);
    postValue(false);
    Log.d("Connection:", "onLost");
    }

    @Override
  2. ExNDY revised this gist Oct 24, 2020. 1 changed file with 3 additions and 7 deletions.
    10 changes: 3 additions & 7 deletions NetworkConnectionMonitor.java
    Original file line number Diff line number Diff line change
    @@ -18,15 18,11 @@ public NetworkConnectionMonitor(Context context) {
    //Network status monitoring
    public void registerDefaultNetworkCallback() {
    try {
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    //Set connection status then application start. If you need u can delete next 3 lines
    NetworkInfo nInfo = connectivityManager.getActiveNetworkInfo();
    boolean isConnected = nInfo != null && nInfo.isAvailable() && nInfo.isConnected();
    postValue(isConnected);

    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    assert connectivityManager != null;
    callback = new ConnectionNetworkCallback();
    connectivityManager.registerDefaultNetworkCallback(callback);
    connectivityManager.registerDefaultNetworkCallback(callback);
    postValue(false);
    } catch (Exception e) {
    Log.d("Connection: Exception in registerDefaultNetworkCallback", "xD");
    postValue(false);
  3. ExNDY revised this gist Oct 24, 2020. 3 changed files with 5 additions and 9 deletions.
    6 changes: 0 additions & 6 deletions ActivityClass.java
    Original file line number Diff line number Diff line change
    @@ -19,12 19,6 @@ public void onChanged(Boolean isConnected) {
    }
    }
    });
    if (Variables.isNetworkConnected){
    //TODO if online
    }
    else {
    //TODO if offline
    }
    ...
    }
    @Override
    7 changes: 5 additions & 2 deletions NetworkConnectionMonitor.java
    Original file line number Diff line number Diff line change
    @@ -19,6 19,11 @@ public NetworkConnectionMonitor(Context context) {
    public void registerDefaultNetworkCallback() {
    try {
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    //Set connection status then application start. If you need u can delete next 3 lines
    NetworkInfo nInfo = connectivityManager.getActiveNetworkInfo();
    boolean isConnected = nInfo != null && nInfo.isAvailable() && nInfo.isConnected();
    postValue(isConnected);

    assert connectivityManager != null;
    callback = new ConnectionNetworkCallback();
    connectivityManager.registerDefaultNetworkCallback(callback);
    @@ -40,15 45,13 @@ private class ConnectionNetworkCallback extends ConnectivityManager.NetworkCallb
    public void onAvailable(@NonNull android.net.Network network) {
    super.onAvailable(network);
    postValue(true);
    Variables.isNetworkConnected = true;
    Log.d("Connection:", "onAvailable");
    }

    @Override
    public void onLost(@NonNull android.net.Network network) {
    super.onLost(network);
    postValue(false);
    Variables.isNetworkConnected = false;
    Log.d("Connection:", "onLost");
    }

    1 change: 0 additions & 1 deletion Variables.java
    Original file line number Diff line number Diff line change
    @@ -1,5 1,4 @@
    public class Variables {
    //Store status of registration Networkcallback
    public static boolean isRegistered = false;
    public static boolean isNetworkConnected = false;
    }
  4. ExNDY revised this gist Oct 24, 2020. 3 changed files with 14 additions and 6 deletions.
    7 changes: 6 additions & 1 deletion ActivityClass.java
    Original file line number Diff line number Diff line change
    @@ -19,7 19,12 @@ public void onChanged(Boolean isConnected) {
    }
    }
    });

    if (Variables.isNetworkConnected){
    //TODO if online
    }
    else {
    //TODO if offline
    }
    ...
    }
    @Override
    12 changes: 7 additions & 5 deletions NetworkConnectionMonitor.java
    Original file line number Diff line number Diff line change
    @@ -39,15 39,17 @@ private class ConnectionNetworkCallback extends ConnectivityManager.NetworkCallb
    @Override
    public void onAvailable(@NonNull android.net.Network network) {
    super.onAvailable(network);
    postValue(true);
    postValue(true);
    Variables.isNetworkConnected = true;
    Log.d("Connection:", "onAvailable");
    }

    @Override
    @Override
    public void onLost(@NonNull android.net.Network network) {
    super.onLost(network);
    postValue(false);
    Log.d("Connection:", "onLost");
    super.onLost(network);
    postValue(false);
    Variables.isNetworkConnected = false;
    Log.d("Connection:", "onLost");
    }

    @Override
    1 change: 1 addition & 0 deletions Variables.java
    Original file line number Diff line number Diff line change
    @@ -1,4 1,5 @@
    public class Variables {
    //Store status of registration Networkcallback
    public static boolean isRegistered = false;
    public static boolean isNetworkConnected = false;
    }
  5. ExNDY renamed this gist Oct 24, 2020. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  6. ExNDY created this gist Oct 24, 2020.
    39 changes: 39 additions & 0 deletions ActivityClass.java
    Original file line number Diff line number Diff line change
    @@ -0,0 1,39 @@
    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;
    }
    }
    84 changes: 84 additions & 0 deletions NetworkConnectionMonitor.java
    Original file line number Diff line number Diff line change
    @@ -0,0 1,84 @@
    import android.content.Context;
    import android.net.ConnectivityManager;
    import android.net.LinkProperties;
    import android.net.Network;
    import android.net.NetworkCapabilities;
    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;
    }
    //Network status monitoring
    public void registerDefaultNetworkCallback() {
    try {
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    assert connectivityManager != null;
    callback = new ConnectionNetworkCallback();
    connectivityManager.registerDefaultNetworkCallback(callback);
    } catch (Exception e) {
    Log.d("Connection: Exception in registerDefaultNetworkCallback", "xD");
    postValue(false);
    }
    }
    //Destroy
    public void unregisterDefaultNetworkCallback(){
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    assert connectivityManager != null;
    connectivityManager.unregisterNetworkCallback(callback);
    }
    //Custom NetworkCallback
    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");
    }
    }
    }
    4 changes: 4 additions & 0 deletions Variables
    Original file line number Diff line number Diff line change
    @@ -0,0 1,4 @@
    public class Variables {
    //Store status of registration Networkcallback
    public static boolean isRegistered = false;
    }