Android Tutorial – Detect Device Internet Connection Status Programatically


Detecting internet connection status in your app is very important especially when creating a Real Time Android applications. In this article, you will learn how to detect device internet connection using android java. We will achieve this manually and programmatically.

Download Code | Download Application

Also Read: How to Properly place google ads on WordPress website



How to detect device Internet connection status in Android

Video Demo

Go ahead and create a new Android application from the android studio from the File > New Application. Fill in all the necessary details.

First, to detect the internet connection, you will need ACCESS_NETWORK_STATE permission, go ahead and add that in your android manifest file as shown below

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

Permissions should be added in the AndroidManifest.xml file, inside the manifest tag.

Add the following code in your content_main.xml or your activit’s layout file

Also Read: Android SQLite Search – Searching SQLite Database in Android

<?xml version="1.0" encoding="utf-8"?>
<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:padding="10dp"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_main">

    <Button
        android:id="@+id/detect_internet_connections"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Detect Internet Connections" />

</RelativeLayout>

The layout contains only one button. We shall be clicking this button, to detect if there is an internet connection or not.



Also Read: How to create android settings screen using PreferenceFragment

Add the following code in your MainActivity.java or your activity’s class.

package hack.hacksmile.hacksmiletutorials;

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

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        Button button = findViewById(R.id.detect_internet_connections);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);

                NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

                if (networkInfo != null && networkInfo.isConnected()) {
                    Toast.makeText(MainActivity.this, "Great Connected", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, "No internet connections", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

}

That’s how to detect internet connection status in any android device programmatically. Feel free to ask any question in the commenting system below.