How to check different Network status in Android?

By | May 5, 2012

This code snippet checks which of your networks are available in android.

package com.coderzheaven.pack;

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

public class CheckConnectionsDemo extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        checkAvailableConnection();
    }
    
    void checkAvailableConnection()
    {
	    ConnectivityManager connMgr = (ConnectivityManager)
	    this.getSystemService(Context.CONNECTIVITY_SERVICE);	
	
	    final android.net.NetworkInfo wifi =
	    connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);	
	
	    final android.net.NetworkInfo mobile =
	    connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
	
	    if( wifi.isAvailable() ){
	    	Toast.makeText(this, "Wifi Available" , Toast.LENGTH_LONG).show();
	    }
	    else if( mobile.isAvailable() ){
	    	Toast.makeText(this, "3G Available" , Toast.LENGTH_LONG).show();
	    }
	    else{	
	    	Toast.makeText(this, "No Network Available" , Toast.LENGTH_LONG).show();
	    }
    }
}

One thought on “How to check different Network status in Android?

  1. Arindom

    not run this code .

    here is errorr :

    package com.coderzheaven.pack;
    Syntax error on tokens, SimpleName expected instead.

    Reply

Leave a Reply

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