Wednesday, 11 July 2012

Check wifi Connection Status in Android Programatically

So many people think that how to find wifi connection status in Android.

In AndroidSDk Provide ConnectivityManager Class for all queries about

the state of network connectivity.

and also notifies applications when network connectivity changes.

This Example i will explain how to detect wifi connection status in your applications. 
To find status of wifi the below permissions are mandatory in your AndroidManifest file
  <!-- Wifi state Permissions -->
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

    <!-- Network State Permissions -->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />


UI Layout(check_wifi_connection.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:id="@+id/aboutapp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="This Example Expains Check wifi connection status"
        android:textSize="15sp" />
    <Button
        android:id="@+id/checkstatus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="44dp"
        android:text="Click me i will tell you" />
</RelativeLayout>

Android Activity(CheckWifiConnection .java)

In Activity isWifiAvailable() method check wifi connection status and return true if your device have a wifi connection else return false.

To find status of wifi connection

we use ConnectivityManager in isWifiAvailable() method

package com.androidsurya.checkwificonnection;

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

public class CheckWifiConnection extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.check_wifi_connection);
        Button check = (Button) findViewById(R.id.checkstatus);
        check.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if (isWifiAvailable()) {
                    Toast.makeText(getApplicationContext(),
                            "You have WIFI connection", Toast.LENGTH_LONG)
                            .show();
                } else {

                    Toast.makeText(getApplicationContext(),
                            "You don't have WIFI connection", Toast.LENGTH_LONG)
                            .show();
                }
            }
        });
    }

    public Boolean isWifiAvailable() {

        try {
            ConnectivityManager connectivityManager = (ConnectivityManager) getApplicationContext()
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo wifiInfo = connectivityManager
                    .getNetworkInfo(ConnectivityManager.TYPE_WIFI);

            if (wifiInfo.isConnected()) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.check_internet_connection, menu);
        return true;
    }

}

Register Activity in AndroidManifest file

<activity
            android:name="com.androidsurya.checkwificonnection.CheckWifiConnection"
            android:label="@string/app_name" >

Output Screenshot







For more information about ConnectivityManager: Android Developers

No comments:

Post a Comment

Android SQLite Database Viewer or Debuging with Stetho

Every Android Developer uses SQLite Database to store data into the Android Application data. But to view the data in SQLite have a lot of...