Recently Added

6/recent/ticker-posts

Check Internet Connection Using Broadcast Receiver Continuously in Android Studio 2021

In this blog, I'm going to create how we can check internet connection continuously with the help of a Broadcast receiver in the android studio 
so let's start



1. Create a new Android Studio Project

2.Follow my Tutorials to implement the XML part.
   
Here is the activity_main.xml
<?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">

<Button
android:id="@+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="check"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>



Here is the custom_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">


<com.airbnb.lottie.LottieAnimationView
android:layout_width="match_parent"
android:layout_height="500dp"
app:lottie_autoPlay="true"
app:lottie_loop="true"
app:lottie_rawRes="@raw/internet_no" />


<Button
android:id="@+id/retry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="retry" />

</LinearLayout>


2.Create NetworkUtil.java File and add this code
package com.example.checkconnection;

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

public class NetworkUtil {


public static String getNetworkState(Context context) {
String status = null;
ConnectivityManager connectivityManager =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo != null) {

if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
status = "Wifi connected";
return status;
} else if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
status = "Mobile data connected";
return status;
}


} else {
status = "No internet";
return status;
}

return status;
}
}

3.Create InternetCheckService.java File and add this code
package com.example.checkconnection;

import android.app.Activity;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Button;
import android.widget.Toast;

public class InternetCheckService extends BroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {
String status = NetworkUtil.getNetworkState(context);
Dialog dialog = new Dialog(context,
android.R.style.Theme_NoTitleBar_Fullscreen);
dialog.setContentView(R.layout.custom_dialog);
Button rettry = dialog.findViewById(R.id.retry);

rettry.setOnClickListener(v -> {
((Activity) context).finish();
Intent intent1 = new Intent(context, MainActivity.class);
context.startActivity(intent1);
});


if (status.isEmpty() || status.equals("No internet")) {
dialog.show();
}


Toast.makeText(context, status, Toast.LENGTH_SHORT).show();


}
}

4.Now Add code in MainActivity.java 
package com.example.checkconnection;

import android.content.BroadcastReceiver;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

BroadcastReceiver broadcastReceiver = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

broadcastReceiver = new InternetCheckService();
checkInternet();

}

private void checkInternet() {
registerReceiver(broadcastReceiver,
new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}

@Override
protected void onPause() {
super.onPause();
unregisterReceiver(broadcastReceiver);
}
}


4.Now Open AndroidManifest File and add permission


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



And also add this line in <application> here paste the code </application>


<receiver android:name=".InternetCheckService">
<intent-filter>
<action android:name="android.net.wifi.WIFI_STATE_CHANGED"/>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"
tools:ignore="BatteryLife" />
</intent-filter>
</receiver>

Follow my Video Tutorials 


Post a Comment

0 Comments