check internet connection in android

Internet connection checking in android device

Internet connection checking in android device

In our some project we need network to connect with remote server. Before network calling we need to check network is available or not in device. Use the shortcode below, before use add permission ‘ACCESS_NETWORK_STATE’ in your manifest file.

Read How to add permission to manifest file in android.

Permission :-

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

Shortcode:-

 

public static boolean checkNetwork(Context context)

       {

              ConnectivityManager connManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);

              TelephonyManager teleManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);

              NetworkInfo netInfo = connManager.getActiveNetworkInfo();

              if (netInfo == null ||!connManager.getBackgroundDataSetting())

              {

                     return false;

              }

              int type = netInfo.getType();

              int subType = netInfo.getSubtype();

              if (type == ConnectivityManager.TYPE_WIFI || type == ConnectivityManager.TYPE_MOBILE)

              {

                     return netInfo.isConnected();

              } else if (type == ConnectivityManager.TYPE_MOBILE && subType == TelephonyManager.NETWORK_TYPE_UMTS && !teleManager.isNetworkRoaming())

              {

                     return netInfo.isConnected();

              } else

              {

                     return false;

              }

       }

Source Code:-


CheckNetworkActivity.java


package com.androidfromhome.checknetworkconnection;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class CheckNetworkActivity extends Activity implements OnClickListener {

    private Button btn_check;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_network);
        
        btn_check=(Button)findViewById(R.id.btn_check);
        btn_check.setOnClickListener(this);
    }
    
    
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        
        switch (v.getId()) {
        case R.id.btn_check:
            if (checkNetwork(CheckNetworkActivity.this)) {
                Toast.makeText(CheckNetworkActivity.this, "Network Available", Toast.LENGTH_LONG).show();    
                }else{
                Toast.makeText(CheckNetworkActivity.this, "Network Connection Error", Toast.LENGTH_LONG).show();    
                }
            break;

        default:
            break;
        }
        
    }

    @SuppressWarnings("deprecation")
    public static boolean checkNetwork(Context context)
    {
        ConnectivityManager connManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        TelephonyManager teleManager =  (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);

        NetworkInfo netInfo = connManager.getActiveNetworkInfo();
        if (netInfo == null ||!connManager.getBackgroundDataSetting())
        {
            return false;
        }
        
        int type = netInfo.getType();
        int subType = netInfo.getSubtype();
    
        if (type == ConnectivityManager.TYPE_WIFI || type == ConnectivityManager.TYPE_MOBILE)
        {
            return netInfo.isConnected();
        } else if (type == ConnectivityManager.TYPE_MOBILE && subType == TelephonyManager.NETWORK_TYPE_UMTS && !teleManager.isNetworkRoaming())
        {
            return netInfo.isConnected();
        } else
        {
            return false;
        }
    }
   
}


activity_check_network.xml


 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.androidfromhome.checknetworkconnection.CheckNetworkActivity" >

    <LinearLayout
        android:id="@+id/LinearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#A758AB"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="15dp" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="2dp"
            android:text="Android From Home"
            android:textColor="#ffffff"
            android:textSize="22dp"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:gravity="center"
        android:orientation="vertical" >

        <Button
            android:id="@+id/btn_check"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#343434"
            android:padding="10dp"
            android:text="Check Network Connection"
            android:textColor="#ffffff"
            android:textStyle="bold" />

    </LinearLayout>

</LinearLayout>


Download Full Source Code

Related Post:-

Android email send example

ListView in android example with custom layout

How Retrieve contact list in android

Switching One activity  to another activity in Android by intent

Leave a Reply

Your email address will not be published. Required fields are marked *

Get Widget