Monday, 25 April 2011

How to Check Internet Connection in Android?

First Create New Android Project and Give the Project Name "NetworkCheckDemo".

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:id="@+id/txtviewstatus"
        android:text="Dipak Keshariya" android:textSize="24dip"
        android:textStyle="bold" android:layout_gravity="center" />
</LinearLayout>


NetworkCheckDemo.java

package com.android.networkcheckdemo;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class NetworkCheckDemo extends Activity
{
    TextView tvstatus;
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tvstatus=(TextView)findViewById(R.id.txtviewstatus);
        ConnectivityManager cn=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo nf=cn.getActiveNetworkInfo();
        if(nf != null && nf.isConnected()==true )
        {
            Toast.makeText(this, "Network Available", Toast.LENGTH_LONG).show();
            tvstatus.setText("Network Available");
        }
        else
        {
            Toast.makeText(this, "Network Not Available", Toast.LENGTH_LONG).show();
            tvstatus.setText("Network Not Available");
        }
    }
}

Add below 3 permissions in Android Manifest File.

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


After changes in manifest file your AndroidManifest file look like this.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.drc.Video" android:versionCode="1" android:versionName="1.0">
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"></uses-permission>
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".NetworkCheckDemo" 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>
    <uses-sdk android:minSdkVersion="8" />
</manifest>


Then Save it & Run the Program.

The Output is look like this.

if Connection is Available :-

if Connection is Not Avaiolable :-


then the checking is over and now you can check internet connection successfully using this code

Don’t forget to provide feedback or follow this blog, if you find this blog is useful.


1 comment:

  1. Thank you

    But we do I need this permission

    ReplyDelete