Pages

Friday, December 21, 2012

Android Turn ON wifi programmatically

Android Turn ON wifi  programmatically

Coding for Enable WIFI programmatically.
Activity.class

import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.widget.TextView;

public class WifiEnableActivity extends Activity {
    /** Called when the activity is first created. */
    private WifiManager wifiManager; 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE); 
       TextView text = (TextView)findViewById(R.id.txtWiFi);
          wifiManager.setWifiEnabled(true); 
          if(wifiManager.isWifiEnabled()){
              text.setText("Wifi State Enabled");
          }
        
    }
}
Then  some permission and user feature required in manifest file.
  <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission> 
    <uses-permission android:name="android.permission.UPDATE_DEVICE_STATS"></uses-permission> 
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission> 
    <uses-feature android:name="android.hardware.wifi.direct" />
    <uses-permission    android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>

Download Source Code