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

Get Selected Item In ListView Android

The impact of lockdown on mobile gamers behaviors

Testing Realm Migrations.

Android Core interview questions part 1

Espresso Testing with Hilt and MockWebServer

Typical ANR in Android

Android : Vulnerability is inevitable | Part 2

Introduction to RxJava

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

Data is Scrollable or not in WebView Android?

Setting up Mapbox SDK 10.1.0 in Android Studio (2021)

RXJava Filtering Operator

Received status code 502 from server: Bad Gateway Enable Gradle ‘offline mode’ and sync project