Check network connection Java developer

This is the code for checking network connection and rise condition to true or false

//Needs RequestNetwork to work!
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
connected = (netInfo != null && netInfo.isConnectedOrConnecting());
if (connected) {
SketchwareUtil.showMessage(getApplicationContext(), “Connected!”);
}
else {
SketchwareUtil.showMessage(getApplicationContext(), “Not connected!”);

There are two profit in this code. 1st you can check network connection and 2nd is performed any action with true and false.

To perform any action just replace your code with set text block.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.