Android tutorial - Android Wifi - android app development - android studio - android development tutorial



Android wifi

Learn android - android tutorial - Android wifi - android examples - android programs

What is Wifi ?

  • Wi-Fi is the name of a popular wireless networking technology that uses radio waves to provide wireless high-speed Internet and network connections. A common misconception is that the term Wi-Fi is short for "wireless fidelity," however this is not the case.
  • Wi-Fi networks have no physical wired connection between sender and receiver by using radio frequency (RF ) technology a frequency within the electromagnetic spectrum associated with radio wave propagation.
  • When an RF current is supplied to an antenna, an electromagnetic field is created that then is able to propagate through space.
  • android wifi
  • The android.net.wifi.WifiManager class can be used to manage the wifi connectivity. It can be used to add network, disable network, scan for access points, disconnect etc.

Android wifi example to enable and disable wifi

  • Let's see the simple example of wifi to enable and disable the wifi service.

activity_main.xml

  • File: activity_main.xml
<RelativeLayout xmlns:androclass="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=".MainActivity" >  
  
    <Button  
        android:id="@+id/button1"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignParentLeft="true"  
        android:layout_alignParentTop="true"  
        android:layout_marginLeft="76dp"  
        android:layout_marginTop="67dp"  
        android:text="Enable Wifi" />  
  
    <Button  
        android:id="@+id/button2"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignLeft="@+id/button1"  
        android:layout_below="@+id/button1"  
        android:layout_marginTop="44dp"  
        android:text="Disable Wifi" />  
  
</RelativeLayout> 
click below button to copy the code from android tutorial team

Activity class

  • File: MainActivity.java
package com.example.wifi;  
  
import android.net.wifi.WifiManager;  
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;  
  
public class MainActivity extends Activity {  
    Button enableButton,disableButton;  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
          
        enableButton=(Button)findViewById(R.id.button1);  
        disableButton=(Button)findViewById(R.id.button2);  
          
        enableButton.setOnClickListener(new OnClickListener(){  
            public void onClick(View v){  
                WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);  
                wifi.setWifiEnabled(true);  
            }  
        });  
        disableButton.setOnClickListener(new OnClickListener(){  
            public void onClick(View v){  
                WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);  
                wifi.setWifiEnabled(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.activity_main, menu);  
        return true;  
    }  
  
}  
click below button to copy the code from android tutorial team

Add Permission in AndroidManifest.xml

  • You need to add following permissions in AndroidManifest.xml file.
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />  
<uses-permission android:name="android.permission.INTERNET" />  
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>  
click below button to copy the code from android tutorial team

Output:

 output of wifi

Related Searches to Android Wifi