How to check internet connection from your Android App?

In this video is shows the steps to check whether the internet is available or accessible from your phone or not. For check the internet accessibility, in this App it uses the Inet API to fetch the IP address of any website or server. In this video it tries to access http://www.google.com and get its Inet Address.

On receiving the Inet address it prints the output on a text view in the layout.

Please note, there are various technique to check the accessibility of the internet from your phone. Getting inet address is just one approach and probably the easiest and quickest way to find the internet access.

I hope you liked this video. For any questions, suggestions or appreciations, please reach out to us at:
https://programmerworld.co/contact/ or write to us at: programmerworld1990@gmail.com

Source Code:

package com.programmerworld.checkmyinternet;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.TextView;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class MainActivity extends AppCompatActivity {

private TextView textView;

private String stringNetwork = "No Internet";
private Thread thread;

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

textView = findViewById(R.id.textView);
}

private void threadMethod(){

try {
InetAddress inetAddress = InetAddress.getByName("www.google.com");

if (!inetAddress.equals("")){
stringNetwork = inetAddress.toString();
return;
}

} catch (UnknownHostException e) {
e.printStackTrace();
}

stringNetwork = "No Internet";
}

public void internetCheckButtonClick(View view){
thread = new Thread(new Runnable() {
@Override
public void run() {
threadMethod();
}
});

thread.start();

SystemClock.sleep(1000);

textView.setText(stringNetwork);
}
}

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.programmerworld.checkmyinternet">

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

<?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">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="124dp"
android:layout_marginTop="116dp"
android:onClick="internetCheckButtonClick"
android:text="@string/check_internet"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Leave a Reply

%d bloggers like this: