Cheap VPS & Xen Server

Residential Proxy Network - Hourly & Monthly Packages

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
  1. <RelativeLayout xmlns:androclass=“http://schemas.android.com/apk/res/android”
  2.     xmlns:tools=“http://schemas.android.com/tools”
  3.     android:layout_width=“match_parent”
  4.     android:layout_height=“match_parent”
  5.     tools:context=“.MainActivity” >
  6.     <Button
  7.         android:id=“@+id/button1”
  8.         android:layout_width=“wrap_content”
  9.         android:layout_height=“wrap_content”
  10.         android:layout_alignParentLeft=“true”
  11.         android:layout_alignParentTop=“true”
  12.         android:layout_marginLeft=“76dp”
  13.         android:layout_marginTop=“67dp”
  14.         android:text=“Enable Wifi” />
  15.     <Button
  16.         android:id=“@+id/button2”
  17.         android:layout_width=“wrap_content”
  18.         android:layout_height=“wrap_content”
  19.         android:layout_alignLeft=“@+id/button1”
  20.         android:layout_below=“@+id/button1”
  21.         android:layout_marginTop=“44dp”
  22.         android:text=“Disable Wifi” />
  23. </RelativeLayout>

Activity class

File: MainActivity.java
  1. package com.example.wifi;
  2. import android.net.wifi.WifiManager;
  3. import android.os.Bundle;
  4. import android.app.Activity;
  5. import android.content.Context;
  6. import android.view.Menu;
  7. import android.view.View;
  8. import android.view.View.OnClickListener;
  9. import android.widget.Button;
  10. public class MainActivity extends Activity {
  11.     Button enableButton,disableButton;
  12.     @Override
  13.     protected void onCreate(Bundle savedInstanceState) {
  14.         super.onCreate(savedInstanceState);
  15.         setContentView(R.layout.activity_main);
  16.         enableButton=(Button)findViewById(R.id.button1);
  17.         disableButton=(Button)findViewById(R.id.button2);
  18.         enableButton.setOnClickListener(new OnClickListener(){
  19.             public void onClick(View v){
  20.                 WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
  21.                 wifi.setWifiEnabled(true);
  22.             }
  23.         });
  24.         disableButton.setOnClickListener(new OnClickListener(){
  25.             public void onClick(View v){
  26.                 WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
  27.                 wifi.setWifiEnabled(false);
  28.             }
  29.         });
  30.     }
  31.     @Override
  32.     public boolean onCreateOptionsMenu(Menu menu) {
  33.         // Inflate the menu; this adds items to the action bar if it is present.
  34.         getMenuInflater().inflate(R.menu.activity_main, menu);
  35.         return true;
  36.     }
  37. }

Add Permission in AndroidManifest.xml

You need to add following permissions in AndroidManifest.xml file.

  1. <uses-permission android:name=“android.permission.ACCESS_WIFI_STATE” />
  2. <uses-permission android:name=“android.permission.INTERNET” />
  3. <uses-permission android:name=“android.permission.CHANGE_WIFI_STATE”/>

download this android example

Output:

wifioutput1

Comments

comments