Continuously check Internet Connection and connection type

Internet connectivity broadcast receiver

NetworkUtills

package com.saud_khan_dev.speed_test.backend.network_utills;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class NetworkUtil {
public static final int TYPE_WIFI = 1;
public static final int TYPE_MOBILE = 2;
public static final int TYPE_NOT_CONNECTED = 0;
public static final int NETWORK_STATUS_NOT_CONNECTED = 0;
public static final int NETWORK_STATUS_WIFI = 1;
public static final int NETWORK_STATUS_MOBILE = 2;

public static int getConnectivityStatus(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (null != activeNetwork) {
if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
return TYPE_WIFI;

if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
return TYPE_MOBILE;
}
return TYPE_NOT_CONNECTED;
}

public static int getConnectivityStatusString(Context context) {
int conn = NetworkUtil.getConnectivityStatus(context);
int status = 0;
if (conn == NetworkUtil.TYPE_WIFI) {
status = NETWORK_STATUS_WIFI;
} else if (conn == NetworkUtil.TYPE_MOBILE) {
status = NETWORK_STATUS_MOBILE;
} else if (conn == NetworkUtil.TYPE_NOT_CONNECTED) {
status = NETWORK_STATUS_NOT_CONNECTED;
}
return status;
}
}

Network change broadcast receiver

package com.saud_khan_dev.speed_test.backend.broadcast_receivers;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

import com.saud_khan_dev.speed_test.backend.network_utills.NetworkUtil;

public class NetworkChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int status = NetworkUtil.getConnectivityStatusString(context);
int connectionType = NetworkUtil.getConnectivityStatus(context);
Log.e("network reciever", "network reciever");
if ("android.net.conn.CONNECTIVITY_CHANGE".equals(intent.getAction())) {
if (status == NetworkUtil.NETWORK_STATUS_NOT_CONNECTED) {
Toast.makeText(context, "Not connected", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "Connected", Toast.LENGTH_SHORT).show();
}
if (connectionType==NetworkUtil.TYPE_MOBILE){
Toast.makeText(context, "Mobile data", Toast.LENGTH_SHORT).show();
}else if (connectionType==NetworkUtil.TYPE_WIFI){
Toast.makeText(context, "Wifi", Toast.LENGTH_SHORT).show();
}
}
}
}

Required Permissions

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

Put it in manifest file

<receiver
android:name=".backend.broadcast_receivers.NetworkChangeReceiver"
android:label="NetworkChangeReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
</intent-filter>
</receiver>

--

--

--

I’m a good husband, father and Senior Developer.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Kotlin Collection Functions

How to Create Custom Toast in Android

Activity Lifecycle-Android

Wear OS [All official development resources]

Automatic SMS Verification with SMS User Consent

Gracefully handling Android app crashes

GameDevHQ Crash Course #36 onClick Events

Android app size after Flutter module integration

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
CodingwithSaud

CodingwithSaud

I’m a good husband, father and Senior Developer.

More from Medium

Espresso UI Testing for Intents in Android

Two mistakes when I use AutoValue

Notes on Android Dev — Lecture 1 — Why/What/How Android

Android & How to secure it?!