Last active
November 18, 2020 17:15
-
-
Save ExNDY/2c27b9c29b9642accbb0cd4f33ca9421 to your computer and use it in GitHub Desktop.
Revisions
-
ExNDY revised this gist
Oct 24, 2020 . 1 changed file with 29 additions and 15 deletions.There are no files selected for viewing
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 charactersOriginal 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; } 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 -
ExNDY revised this gist
Oct 24, 2020 . 1 changed file with 3 additions and 7 deletions.There are no files selected for viewing
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 charactersOriginal 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); assert connectivityManager != null; callback = new ConnectionNetworkCallback(); connectivityManager.registerDefaultNetworkCallback(callback); postValue(false); } catch (Exception e) { Log.d("Connection: Exception in registerDefaultNetworkCallback", "xD"); postValue(false); -
ExNDY revised this gist
Oct 24, 2020 . 3 changed files with 5 additions and 9 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -19,12 19,6 @@ public void onChanged(Boolean isConnected) { } } }); ... } @Override 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 charactersOriginal 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); Log.d("Connection:", "onAvailable"); } @Override public void onLost(@NonNull android.net.Network network) { super.onLost(network); postValue(false); Log.d("Connection:", "onLost"); } 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 charactersOriginal 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; } -
ExNDY revised this gist
Oct 24, 2020 . 3 changed files with 14 additions and 6 deletions.There are no files selected for viewing
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 charactersOriginal 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 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 charactersOriginal 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); 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"); } @Override 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 charactersOriginal 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; } -
ExNDY renamed this gist
Oct 24, 2020 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
ExNDY created this gist
Oct 24, 2020 .There are no files selected for viewing
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 charactersOriginal 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; } } 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 charactersOriginal 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"); } } } 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 charactersOriginal 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; }