Saturday, 14 July 2012

Enable/Disable WiFi in Android Programatically

How to Enable/Disable WiFi  Programatically in Android.(Android WiFi API)

In Android using WifiManger Class we can find WiFi state and we can Enable/Disable WiFi.

To Enable WiFi

WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifi.setWifiEnabled(true);

To Disable WiFi


WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifi.setWifiEnabled(false);

Note: To access with WiFi state, we have to add following permissions inside the AndroidManifest.xml file:

android.permission.ACCESS_WIFI_STATE
android.permission.UPDATE_DEVICE_STATS
android.permission.CHANGE_WIFI_STATE

For More Information about WifiManager Android Developers

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

Monday, 2 July 2012

Check Internet Connection Status in Android

So many people think that how to find internet 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 internet connection status in your applications.
To find status of network state the below permissions are mandatory in your AndroidManifest file

<!-- Internet Permissions -->
    <uses-permission android:name="android.permission.INTERNET" />

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

UI Layout(check_internet_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"
    tools:context=".CheckInternetConnection" >

    <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 internet 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(CheckInternetConnection.java)


In Activity isconnection() method check internet connection status and return true if your mobile have a internet connection else return false.

To find status of internet connection
we use ConnectivityManager in isconnection() method

package com.androidsurya.checkinternetconnection;

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

public class CheckInternetConnection extends Activity {

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

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

                    Toast.makeText(getApplicationContext(),
                            "Your Mobile don't have internet connection",
                            Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    public boolean isConnecting() {
        ConnectivityManager connectivity = (ConnectivityManager) getApplicationContext()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null)
                for (int i = 0; i < info.length; i++)
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }

        }
        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.checkinternetconnection.CheckInternetConnection"
            android:label="@string/app_name" >

Output Screenshot:








For more information about ConnectivityManager: Android Developers

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...