This example demonstrates how to check the state of the internet connection through the broadcast Receiver.
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project, and fill in all required details to create a new project.
Step 2 − To find the internet status we have to add network state permission to the AndroidManifest.xml file as shown below.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Step 3 − Now create a new class name ConnectionRecever.java and put this code on it.
package com.example.myapp;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
import androidx.core.content.ContextCompat;
import com.google.android.material.snackbar.Snackbar;
public class ConnectionRecever extends BroadcastReceiver {
private static int Value = 1;
@SuppressLint("ResourceAsColor")
@Override
public void onReceive(Context context, Intent intent) {
if (isConnected(context)){
if (Value > 1){
Snackbar snackbar = Snackbar.make(((Activity) context).findViewById(android.R.id.content), "online!!!", Snackbar.LENGTH_SHORT);
snackbar.setBackgroundTint(ContextCompat.getColor(context,R.color.green));
View mView = snackbar.getView();
TextView mTextView = (TextView) mView.findViewById(com.google.android.material.R.id.snackbar_text);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
mTextView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
} else {
mTextView.setGravity(Gravity.CENTER_HORIZONTAL);
}
snackbar.show();
Value--;
}
}
else {
Snackbar snackbar = Snackbar.make(((Activity) context).findViewById(android.R.id.content), "Internet not connected!!!", Snackbar.LENGTH_INDEFINITE);
snackbar.setBackgroundTint(ContextCompat.getColor(context,R.color.red));
View mView = snackbar.getView();
TextView mTextView = (TextView) mView.findViewById(com.google.android.material.R.id.snackbar_text);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
mTextView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
} else {
mTextView.setGravity(Gravity.CENTER_HORIZONTAL);
}
snackbar.show();
Value++;
}
}
public boolean isConnected(Context context){
try {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
return (networkInfo != null && networkInfo.isConnected());
}catch (NullPointerException e){
e.printStackTrace();
return false;
}
}
}
Step 4 – Now back to the MainActivity.java file and get the functionality of ConnectionRecever class
package com.example.myapp;
import androidx.appcompat.app.AppCompatActivity;
import android.content.BroadcastReceiver;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Build;
import android.os.Bundle;
import com.itfox.bgmiguideforbattlegrounds.databinding.ActivityMainBinding;
public class MainActivity extends AppCompatActivity{
//Variable
BroadcastReceiver broadcastReceiver;
ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
broadcastReceiver = new ConnectionRecever();
registerNetworkBrodcast();
}
protected void registerNetworkBrodcast() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
registerReceiver(broadcastReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}
}
}