Check Internet Connected or not

In this tutorial, we are going to learn How to Check Internet Connected to phone (Android) or not. Nowadays almost all of the apps need to connect to the internet for a purpose. It will help you to check the internet connection before performing some action and show some error to the client.

The App look Like

GitHub: https://github.com/rincemathew/CheckinternetConnectedornot

Tips:
This method actually checks if the device is connected to the internet(There is a possibility it’s connected to a network but not to the internet). If you are connected to a WiFi network that doesn’t include internet access or requires browser-based authentication, connected will still be true.

Open AndroidManifest.xml and add ‘ACCESS_NETWORK_STATE’ permission.

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

Create a Layout File

In here we have used a Button with an Id ‘checkInternet‘.
Open activity_main.xml and paste the following code,

activity_main.xml

<RelativeLayout 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"
    android:gravity="center"
    tools:context=".MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Check internet connection"
        android:id="@+id/checkInternet" />

</RelativeLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {


    Button checkInternet;

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

        checkInternet=findViewById(R.id.checkInternet);
        checkInternet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final ConnectivityManager conMgr = (ConnectivityManager)getSystemService(getApplicationContext().CONNECTIVITY_SERVICE);
                final NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo();

                if (activeNetwork != null && activeNetwork.isConnected()){
                    Toast.makeText(MainActivity.this,"You are connected to Internet",Toast.LENGTH_SHORT).show();
                }
                else {
                    Toast.makeText(MainActivity.this,"You are not connected to Internet",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}


Test your app by Turning off the Internet and Clicking on the Check Internet Connection button. and connect to the internet by turning ON your Wifi or Mobile Data and Click on the Button again, You will get a message either “You are connected to internet” or “You are not connected to internet“.

2 Comments

Add a Comment

Your email address will not be published. Required fields are marked *