AdSense

AdSense3

Friday 30 October 2015

Android Wifi Example

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.   
  7.     <Button  
  8.         android:id="@+id/button1"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignParentLeft="true"  
  12.         android:layout_alignParentTop="true"  
  13.         android:layout_marginLeft="76dp"  
  14.         android:layout_marginTop="67dp"  
  15.         android:text="Enable Wifi" />  
  16.   
  17.     <Button  
  18.         android:id="@+id/button2"  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content"  
  21.         android:layout_alignLeft="@+id/button1"  
  22.         android:layout_below="@+id/button1"  
  23.         android:layout_marginTop="44dp"  
  24.         android:text="Disable Wifi" />  
  25.   
  26. </RelativeLayout>  

Activity class

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

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"/>  

Output:

android wifi example output 1

No comments:

Post a Comment