Wednesday, July 18, 2012

How to Check Android Network / Internet Connectivity Status

6 comments

It is not complex to Check Android Network / Internet Connectivity Status, bellow DetectConnection class
will help you to check Android Network / Internet Connectivity Status. Here I am going to load Android Wireless & Network Setting if device is not connected to any Network.



check network  internet connection in android



CheckConnectionActivity Activity will load Wireless & Network Setting if your Android device is not connected to the Network.


package com.jsupport;

import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.app.Activity;
import android.content.Intent;

public class CheckConnectionActivity extends Activity {

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.check_connection);

  Button check_connection = (Button) findViewById(R.id.btn_checkConnection);

  check_connection.setOnClickListener(new View.OnClickListener() {

   @Override
   public void onClick(View v) {
    if (DetectConnection
      .checkInternetConnection(CheckConnectionActivity.this)) {
     Toast.makeText(CheckConnectionActivity.this,
       "You have Internet Connection", Toast.LENGTH_LONG)
       .show();
    } else {
     Toast.makeText(CheckConnectionActivity.this,
       "You Do not have Internet Connection",
       Toast.LENGTH_LONG).show();

     CheckConnectionActivity.this.startActivity(new Intent(
       Settings.ACTION_WIRELESS_SETTINGS));
    }
   }
  });
 }
}


Create DetectConnection class

Context.getSystemService(Context.CONNECTIVITY_SERVICE)
which will gives the Network information, network availability and the connectivity status of the device


package com.jsupport;

package com.jsupport;

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

public class DetectConnection {

 public static boolean checkInternetConnection(Context context) {

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

  if (con_manager.getActiveNetworkInfo() != null
    && con_manager.getActiveNetworkInfo().isAvailable()
    && con_manager.getActiveNetworkInfo().isConnected()) {
   return true;
  } else {
   return false;
  }
 }
}


Add following permission to your AndroidManifest.xml to allow internet connection and to access Network Settings


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


AndroidManifest.xml



how to check network internet connection in android
launch android wirless and networks seting







You may also like 


6 comments:

Nikos Maravitsas said...

Hi ,

Great blog! Is there an email address I can contact you in private?

Dev said...

Hi Nikos

I'm on google plus

Panda said...

MERECE O COMENTÁRIO DE UM BRASILEIRO.... "SHOW DE BOLA" ESTE TUTORIAL, PARABÉNS!... SIMPLES, DETALHADO, E PONTUAL!

Chathura Wijesinghe said...

Obrigado Achutti

Sandaruwan said...

Thankx :)

Unknown said...

Thanx...its so simple..

Post a Comment