Android Tutorial - How to check whether the internet connectivity is available or not using java code?

android tutorial - how to check whether internet is availabe in you mobile using java code ?


Hey, Readers in this android tutorial post I am going to show you how to check whether the internet connection is available or not, from your application using java code.

What is the need to check the internet connection availability?

No one wants to lose the precious users of their application, you also. If you are developing a networked application that highly relies on the internet means you need to check whether the internet connection is available or not before sending each and every request to the remote server or any remote backend API like firebase.

Prerequisite

  • you must know about how to create new projects and activity if you don't know please go through this tutorial click here

What you are going to learn from this tutorial?

  • How to get connectivity related information.
  • And how to check the internet connectivity is available or not.

Let's Start!

Note : if you dont know to create new project or activity please check this tutorial
Step 1: Fire up the Android Studio and create a new Android project and name it Network Checker.
Step 2: Leave all other project setup settings as default and click finish.

Let's Design the UI 😃

Step 3: to design the UI for our application, open activity_main.xml which is under the folder "res/layout".
Step 4: Delete the default hello world text view also drag and drop a button from the palette(Widget list).
Step 5: Change the button id to chkBtn and text property to Check Internet Connectivity (See the picture for the reference).



android tutorial - how to check whether internet is availabe in you mobile using java code ?

or you can copy this code to the xml file.
DESIGN CODE

<?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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.codebolt.rajakumar.networkchecker.MainActivity">

<Button
android:text="Check Internet Connectivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="201dp"
android:id="@+id/chkBtn" />
</RelativeLayout>

That's all about UI!
DOWNLOAD SOURCE CODE - Link Below the post

OMG, we need to code!

yes, offcourse we designed the UI part of our application it was looking good now come to the important part that makes most of the application "CODE".

Step 5:  Open MainActivity.java which is under the folder java/your package name/MainActivity.java

Step 6: Add the following code to the file.


package com.codebolt.rajakumar.networkchecker;

import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    //Declaration Part
    private Button chkBtn;
    private ConnectivityManager cManager;
    private NetworkInfo networkInfo;

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

        //Initialization - Button
        chkBtn = (Button) findViewById(R.id.chkBtn);

        //Initialization - ConnectivityManager
        cManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);

        //Checking Code (Checks when connection is available or not)
        chkBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                networkInfo = cManager.getActiveNetworkInfo();
                if(networkInfo != null && networkInfo.isConnectedOrConnecting()){
                    Toast.makeText(getApplicationContext(),"Connected to the Internet",Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(getApplicationContext(),"Internet Not available",Toast.LENGTH_SHORT).show();
                }
            }
        });



    }
}


That's all about code.

If you run this application you will get the following output.

android tutorial - how to check whether internet is availabe in you mobile using java code ?

android tutorial - how to check whether internet is availabe in you mobile using java code ?

android tutorial - how to check whether internet is availabe in you mobile using java code ?

Note: If you face any error feel free to ask. post your code in the comment section below and also if you got the output post the screen shot in the comment section below.


Thank you all ! ðŸ’“
like us on facebook - codebolt

Download Source Code

No comments

Powered by Blogger.