Search Tutorials

Monday, 28 October 2013

Enable and Disable WiFi using Program in Android


In this application, we will learn how to enable and disable WiFi using simple code. So create new project and drop only one button on relative layout which will use to enable and disable WiFi and give id button1 to button. The code of android XML file is given below:

Enable and Disable WiFi using Program in AndroidEnable and Disable WiFi using Program in Android
 
<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"
   android:background="#b21"
>
<Button
   android:id="@+id/button1"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_centerHorizontal="true"
   android:layout_centerVertical="true"
   android:onClick="wwf"
   android:text="Wifi On"
   android:textSize="30sp" />
</RelativeLayout>


Now open your Java file and WiFi manager is used to get WiFi service and enable and disable WiFi. This is very simple because only isWiFienabled() method is used to check WiFi is on or off and change the state of WiFi. The code of android Java file is given below with explanation:


package innosen.wwf; //your package name

import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
  WifiManager wm;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
  }
  //this method will call on button click
  public void wwf(View v)
  {
   Button b1=(Button)findViewById(R.id.button1);
   //get Wifi service
   wm=(WifiManager)getSystemService(WIFI_SERVICE);
   //Check Wifi is on or off
   if(wm.isWifiEnabled())
   {
     b1.setText("Wifi ON");
     //enable or disable Wifi
     //for enable pass true value
     //for disable pass false value
     wm.setWifiEnabled(false);
   }
   else
   {
     b1.setText("Wifi OFF");
     wm.setWifiEnabled(true);
    }
  }
}

Now open your AndroidManifest.xml file and take permission to check WiFi state and to change WiFi state. The code of AndroidManifest.xml file is given below:


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="selecm.wwf"
   android:versionCode="1"
   android:versionName="1.0" >
<uses-sdk
   android:minSdkVersion="8"
   android:targetSdkVersion="10" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<application
   android:allowBackup="true"
   android:icon="@drawable/ic_launcher"
   android:label="@string/app_name"
   android:theme="@style/AppTheme" >
<activity
   android:name="innosen.wwf.MainActivity"
   android:label="@string/app_name" >
   <intent-filter>
     <action android:name="android.intent.action.MAIN" />
     <category android:name="android.intent.category.LAUNCHER" />
   </intent-filter>
   </activity>
  </application>
</manifest>

Now run your application and install .APK file in your mobile and test. You can’t test this application on emulator.
If you have any doubts please comment. Thanks… :)

15 comments:

  1. please tell me where to use java code and xml code thank you

    ReplyDelete
    Replies
    1. Create new project in android eclipse and go to src folder -> package -> Main_Activity.java file and paste all Java code there and than go to res folder -> layout -> main_activity.xml file and paste XML code there...than change your AndroidManifast.xml file according to given above example.

      Delete
  2. Hello Sir thanks for the help..
    But in case we want to again enable and diasble wifi on every click..how to perform that..as nw it jst respond to one click only..

    ReplyDelete
    Replies
    1. just add one more button and use the second turn off service on that button click, so that's so easy.
      In Java:
      wifiOn = (Button) findViewById(R.id.wifiOn);
      wifiOff = (Button) findViewById(R.id.wifiOff);

      final WifiManager wifiManager;

      wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);

      wifiOn.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
      wifiManager.setWifiEnabled(true);
      Toast.makeText(getApplicationContext(),"Wifi is Turned On",Toast.LENGTH_LONG).show();
      }
      });
      wifiOff.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
      wifiManager.setWifiEnabled(false);
      Toast.makeText(getApplicationContext(),"Wifi is Turned Off",Toast.LENGTH_LONG).show();
      }
      });

      Delete
  3. sir, i want to connect more than one wifi at a time to share live streaming video from one android phone to more than one android phone can u help me source code sir

    ReplyDelete
  4. plz could u provide me with the source code of android attendance app using wifi

    ReplyDelete
  5. This comment has been removed by a blog administrator.

    ReplyDelete
  6. hello sir ... there is any method to turf wifi on through sms .. if it is posible then give me code thanks :)

    ReplyDelete
    Replies
    1. Actually it's possible. You'll have to take permission to read sms. Then make some special syntax to read with proper sync

      Delete
  7. Anonymous7:34 pm

    how to find the .APK file to install and thank you. you're the best

    ReplyDelete
  8. Anonymous9:50 am

    getSystemService() marks an error in android studio

    ReplyDelete
    Replies
    1. just write the complete code then error will be removed. or first add permission in Manifest file.

      Delete
  9. sir.
    How to set download localation & Download file within our App.

    ReplyDelete
  10. sir.
    How to connect particular wifi ip address in our app?

    ReplyDelete
  11. Sir, can you please update the code to have a button that makes it cellular network data enabled,?

    ReplyDelete

Back to Top