WiFi Enable/Disable in Programmatically

WifiManager gives connection with default mobiles.It shows Wifi state on/off with programmatically.

activity_main.xml

This xml has only two buttons for enable and disable.




    

    

MainActivity.java

This java file contains wifi manager for get wifi state from default mobiles.

package com.example.icowboysradio.myapplication;

import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;


public class MainActivityWiFi extends AppCompatActivity {

    Button enableButton,disableButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wifi);

        enableButton=(Button)findViewById(R.id.wifienable);
        disableButton=(Button)findViewById(R.id.wifidisable);

        enableButton.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
                wifi.setWifiEnabled(true);
            }
        });

        disableButton.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
                wifi.setWifiEnabled(false);
            }
        });
    }
}