How to Check Internet Connection in Android

Hey, Welcome back to this article. After a long tried I've reached to succeed to test the internet connection in android. It's very latest in 2016. Whatever If you wanna check the data connection and wanna show a toast message or anything I hope you can do after reading this article and following this steps.
So, here we go to check the internet connection and wifi connection.  I will show everything with the screenshot. If you feel any trouble, just comments. I will try to fix your issues.


Step 1:
Open your AndroidManifest.xml file .  Add the three Permissions  in  <manifest</manifest>   but not inside of   <application</application>  


Android Manifest in Android Studio
.  

The 3 permissions are:  

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

To be clear just see the following image :




Step 2:
Create a button in   activity_main.xml  


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    tools:context="com.example.hp.symonapps.MainActivity">


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Check internet"
        android:id="@+id/CheckBtn"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />


</RelativeLayout>



Step 3:
Create a java class.  To create a java class  Right Click on   Java > your package name > New > Java Class



Then got a pop up  . Write the name of java class. Here I write ConnectionDetector. Then click ok. See the image below:




The ConnectionDetector class file has created already.

ConnectionDetector


Open the ConnectionDetector file. Then paste the code below.  Keep your eyes on package name. Don't copy my package name.



ConnectionDetector.java
package com.example.hp.symonapps;

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

public class ConnectionDetector {

    Context context;

    public ConnectionDetector(Context context) {
        this.context = context;
    }


    public boolean isConnected(){
        ConnectivityManager connectivity = (ConnectivityManager)
                context.getSystemService(Service.CONNECTIVITY_SERVICE);

        if(connectivity != null){
            NetworkInfo info = connectivity.getActiveNetworkInfo();
            if(info != null){
                if(info.getState()== NetworkInfo.State.CONNECTED){
                    return true;
                }
            }
        }
        return false;
    } //end isConnected

} //end ConnectionDetector





Now write these codes on MainActivity.java




MainActivity.java
package com.example.hp.symonapps;
        import android.support.v7.app.AppCompatActivity;
        import android.os.Bundle;
        import android.view.View;
        import android.widget.Button;
        import android.widget.Toast;

        import java.util.Random;

public class MainActivity extends AppCompatActivity {

    ConnectionDetector cd;

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

        cd = new ConnectionDetector(this);

        Button b = (Button) findViewById(R.id.CheckBtn);

        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if(cd.isConnected()){
                    Toast.makeText(MainActivity.this,"Yes! Your Data is Connected",Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(MainActivity.this,"opps! Data Not Connected",Toast.LENGTH_LONG).show();
                    }

            }
        });

    } //onCreate end
}

Comments