How to check internet present or not in android

watch_later Friday, August 12, 2016
comment 1 Comment
This tutorial helps about how to check internet present or not in mobile by using ConnectivityManager in android.ConnectivityManager provides all packages for internet providers.
It provides all network connectivity information.internet checking is very easy and simple

Lets begin coding

Create a project

First create an android Project in Eclipse

1) AndroidManifest.xml 

we have to provide some permissions in AndroidManifest.xml for checking internet

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.forbloginternet"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />
   
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
     <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

2) activity_main.xml

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" 
        android:id="@+id/internetcheck"
        android:textSize="28sp"
        android:textStyle="bold"
        android:textColor="@color/material_blue_grey_900"
        android:layout_centerInParent="true"/>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/internetcheck"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="58dp"
         android:textSize="28sp"
        android:textColor="@color/material_blue_grey_900"
        android:text="Network status" />

</RelativeLayout>

3) ConnectionDetector.java

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

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

public boolean isConnectingToInternet(){
ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
 if (connectivity != null) 
 {
 NetworkInfo[] info = connectivity.getAllNetworkInfo();
 if (info != null) 
 for (int i = 0; i < info.length; i++) 
 if (info[i].getState() == NetworkInfo.State.CONNECTED)
 {
 return true;
 }

 }
 return false;
}
}

4)  MainActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {
Boolean isInternetPresent = false;
ConnectionDetector cd;
TextView internetcheck;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
internetcheck=(TextView)findViewById(R.id.internetcheck);
cd = new ConnectionDetector(getApplicationContext());
isInternetPresent = cd.isConnectingToInternet();
if (isInternetPresent) {
internetcheck.setText("Internet Present");
}else
{
internetcheck.setText("Internet not present");
}
}


}

5) Demo

Finally, Run your application

OUTPUT:

Internet present condition:

First enable internet in your testing device then run your application

we will get result shown below

Internet not present condition:

First disable internet in your testing device then run your application

we will get result shown below



avatar

Got your journals and articles index by Google Scholar, Scopus, Web of Science etc. visit http://sch.com.pk for more details.

delete August 21, 2021 at 7:38 AM



sentiment_satisfied Emoticon