Automatically Check Network State With BroadCastReceiver
Intro
Hello again, I hope you had a relaxing weekend? Welcome to the new week!
A new wee, a new opportunity to become world-class - Prosper Otemuyiwa
So, over the weekend I learned something, its a solution to my long term problem, normally when building an app that makes use of the internet to retrieve data, what I'd do as regards ensuring the device is connected to the internet is to use the legendary isConnected function to check and then proceed to execute the next block of codes, I have always wanted a way to notify the user when their internet is down or not connected in real-time not just when they click a particular button.
What's the solution?
I found a tutorial by Coding In Flow that's reports the WiFi states with BroadCastReceiver after seeing how he did that, then I realized the solution to my so-called problem has been there all these years or months sitting and looking at me doing my things. BroadCastReceiver
How To Implement:
Add the following permissions to your AndroidManifest file
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
then in your XML layout file add a TextView that will be used to update the UI based on the network change
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/networkState"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="@string/network_connectivity_state"
android:textColor="#000"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Now let's navigate you our BaseActivity or MainActivity, inside your Activity class
declare the following variables to be used in your code:
private lateinit var networkState: TextView //TextView for showing network status
onCreate(){
...
networkState = findViewById(R.id.networkState)
...
}
then create a Reciever like an example below
private val connectivityReceiver = object : BroadcastReceiver() {
override fun onReceive(p0: Context?, p1: Intent?) {
val cm = p0?.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
cm.registerDefaultNetworkCallback(object : NetworkCallback() {
override fun onAvailable(network: Network) {
runOnUiThread {
kotlin.run {
networkState.text = "You are online" // Update UI
}
}
}
override fun onLost(network: Network) {
runOnUiThread {
kotlin.run {
networkState.text = "You are offline" // Update UI
}
}
}
}
)
} else {
val nwInfo = cm.activeNetworkInfo
val isConnected = nwInfo != null && nwInfo.isConnectedOrConnecting
if (isConnected) {
networkState.text = "You are online"
} else {
networkState.text = "You are offline"
}
}
}
}
Now what we have to do is to register our receiver in our Activity's onStart() to start listening when our activity is started and unregister in onStop() to stop listening when the activity stops.
override fun onStart() {
super.onStart()
val intentFilterNW = IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)
registerReceiver(connectivityReceiver, intentFilterNW)
}
override fun onStop() {
super.onStop()
unregisterReceiver(connectivityReceiver)
}
And that is all we need to do in other to update user about their connectivity status. Code: github.com/chydee/ReportConnectivityState