Skip to content

Instantly share code, notes, and snippets.

@rohitnotes
Forked from Abhinav1217/CheckNetwork.java
Created September 7, 2020 23:20
Show Gist options
  • Save rohitnotes/114d2966897285fcfc88355d9acfcb57 to your computer and use it in GitHub Desktop.
Save rohitnotes/114d2966897285fcfc88355d9acfcb57 to your computer and use it in GitHub Desktop.
Network Test on API 29 - Java
package com.example.simplenetwork;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.LinkProperties;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkRequest;
import android.util.Log;
import androidx.annotation.NonNull;
public class CheckNetwork {
private Context context;
public CheckNetwork(Context context) {
this.context = context;
}
void registerDefaultNetworkCallback(){
try {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
assert connectivityManager != null;
connectivityManager.registerDefaultNetworkCallback(new ConnectivityManager.NetworkCallback(){
@Override
public void onAvailable(@NonNull android.net.Network network) {
super.onAvailable(network);
GlobalVars.isNetworkConnected = true;
Log.d("FLABS:", "onAvailable");
}
@Override
public void onLost(@NonNull android.net.Network network) {
super.onLost(network);
GlobalVars.isNetworkConnected = false;
Log.d("FLABS:", "onLost");
}
@Override
public void onBlockedStatusChanged(@NonNull Network network, boolean blocked) {
super.onBlockedStatusChanged(network, blocked);
Log.d("FLABS:", "onBlockedStatusChanged");
}
@Override
public void onCapabilitiesChanged(@NonNull Network network, @NonNull NetworkCapabilities networkCapabilities) {
super.onCapabilitiesChanged(network, networkCapabilities);
Log.d("FLABS:", "onCapabilitiesChanged");
}
@Override
public void onLinkPropertiesChanged(@NonNull Network network, @NonNull LinkProperties linkProperties) {
super.onLinkPropertiesChanged(network, linkProperties);
Log.d("FLABS:", "onLinkPropertiesChanged");
}
@Override
public void onLosing(@NonNull Network network, int maxMsToLive) {
super.onLosing(network, maxMsToLive);
Log.d("FLABS:", "onLosing");
}
@Override
public void onUnavailable() {
super.onUnavailable();
Log.d("FLABS:", "onUnavailable");
}
});
} catch (Exception e) {
Log.d("FLABS: Exception in registerDefaultNetworkCallback", "hello");
GlobalVars.isNetworkConnected = false;
}
}
void registerNetworkCallback(){
try {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkRequest.Builder builder = new NetworkRequest.Builder();
assert connectivityManager != null;
connectivityManager.registerNetworkCallback(builder.build(), new ConnectivityManager.NetworkCallback(){
@Override
public void onAvailable(@NonNull android.net.Network network) {
super.onAvailable(network);
GlobalVars.isNetworkConnected = true;
Log.d("FLABS:", "onAvailable");
}
@Override
public void onLost(@NonNull android.net.Network network) {
super.onLost(network);
GlobalVars.isNetworkConnected = false;
Log.d("FLABS:", "onLost");
}
@Override
public void onBlockedStatusChanged(@NonNull Network network, boolean blocked) {
super.onBlockedStatusChanged(network, blocked);
Log.d("FLABS:", "onBlockedStatusChanged");
}
@Override
public void onCapabilitiesChanged(@NonNull Network network, @NonNull NetworkCapabilities networkCapabilities) {
super.onCapabilitiesChanged(network, networkCapabilities);
Log.d("FLABS:", "onCapabilitiesChanged");
}
@Override
public void onLinkPropertiesChanged(@NonNull Network network, @NonNull LinkProperties linkProperties) {
super.onLinkPropertiesChanged(network, linkProperties);
Log.d("FLABS:", "onLinkPropertiesChanged");
}
@Override
public void onLosing(@NonNull Network network, int maxMsToLive) {
super.onLosing(network, maxMsToLive);
Log.d("FLABS:", "onLosing");
}
@Override
public void onUnavailable() {
super.onUnavailable();
Log.d("FLABS:", "onUnavailable");
}
});
} catch (Exception e) {
Log.d("FLABS: Exception in registerNetworkCallback", "hello");
GlobalVars.isNetworkConnected = false;
}
}
}
package com.example.simplenetwork;
public class GlobalVars {
public static int counter = 0;
public static boolean isNetworkConnected = false;
}
package com.example.simplenetwork;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Register NetworkCallback -- need to call this when activity starts
CheckNetwork network = new CheckNetwork(getApplicationContext());
/*
* registerNetworkCallback() is available from API-21 and above
* But it will show status of all available network. Hence you would
* to query onCapabilitiesChanged() to make sure your intended network is active or not.
*/
// network.registerNetworkCallback();
/*
* network.registerDefaultNetworkCallback() is available from API-24
* Unlike its counterpart, it will only register status of current active network
* Hence no need to check with onCapabilitiesChanged(). But downside is that it
* is available since API-24 while current recommendation (in 2019) is to maintain
* minimum support for API-23 Marshmallo.
*/
network.registerDefaultNetworkCallback();
final TextView text = findViewById(R.id.test_message);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@SuppressLint({"SetTextI18n", "DefaultLocale"})
@Override
public void onClick(View view) {
GlobalVars.counter++;
if (GlobalVars.isNetworkConnected) {
text.setText(String.format("Counter is %d, And network is Connected", GlobalVars.counter));
} else {
text.setText(String.format("Counter is %d, And network is Not Connected", GlobalVars.counter));
}
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment