Sunday, June 2, 2013

Turn Wifi On/Off using WifiManager.setWifiEnabled() or WifiManager.setWifiDisabled()

Turn Wifi On/Off using WifiManager.setWifiEnabled() or WifiManager.setWifiDisabled()

Step 1:  ADD Or modify main.xml to add to button. in Layout.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >
<TextView 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="@string/hello"
   />
<TextView 
   android:id="@+id/wifistate"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   />
<Button
   android:id="@+id/onwifi"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="Turn Wifi On"
   />
<Button
   android:id="@+id/offwifi"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="Turn Wifi Off"
   />
</LinearLayout> 

Step 2: Add Or change code in your MainActivity Class.

package com.exercise.WifiStateChangedValkesh; 
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class WifiStateChangedValkesh extends Activity {
 
 TextView WifiState;
 
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       WifiState = (TextView)findViewById(R.id.wifistate);
       Button OnWifi = (Button)findViewById(R.id.onwifi);
       Button OffWifi = (Button)findViewById(R.id.offwifi);
      
       this.registerReceiver(this.WifiStateChangedReceiver,
               new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION));
      
       OnWifi.setOnClickListener(new Button.OnClickListener(){

   @Override
   public void onClick(View arg0) {
    // TODO Auto-generated method stub
    WifiManager wifiManager = (WifiManager)getBaseContext().getSystemService(Context.WIFI_SERVICE);
    wifiManager.setWifiEnabled(true);
   }});
      
       OffWifi.setOnClickListener(new Button.OnClickListener(){

   @Override
   public void onClick(View arg0) {
    // TODO Auto-generated method stub
    WifiManager wifiManager = (WifiManager)getBaseContext().getSystemService(Context.WIFI_SERVICE);
    wifiManager.setWifiEnabled(false);
   }});
   }
  
   private BroadcastReceiver WifiStateChangedReceiver
   = new BroadcastReceiver(){

  @Override
  public void onReceive(Context context, Intent intent) {
   // TODO Auto-generated method stub
   
   int extraWifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE ,
     WifiManager.WIFI_STATE_UNKNOWN);
   
   switch(extraWifiState){
   case WifiManager.WIFI_STATE_DISABLED:
    WifiState.setText("WIFI STATE DISABLED");
    break;
   case WifiManager.WIFI_STATE_DISABLING:
    WifiState.setText("WIFI STATE DISABLING");
    break;
   case WifiManager.WIFI_STATE_ENABLED:
    WifiState.setText("WIFI STATE ENABLED");
    break;
   case WifiManager.WIFI_STATE_ENABLING:
    WifiState.setText("WIFI STATE ENABLING");
    break;
   case WifiManager.WIFI_STATE_UNKNOWN:
    WifiState.setText("WIFI STATE UNKNOWN");
    break;
   }
   
  }};
}
 

Step 3: Add permission "android.permission.CHANGE_WIFI_STATE" AndroidManifest.xml

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.exercise.WifiStateChangedValkesh"
     android:versionCode="1"
     android:versionName="1.0">
   <application android:icon="@drawable/icon" android:label="@string/app_name">
       <activity android:name=".WifiStateChangedValkesh"
                 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>
   <uses-sdk android:minSdkVersion="4" />
   <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
</manifest>

No comments:

Post a Comment