Enable Disable WiFi programmatically in android on button click
Hello Guys. Welcome to our new tutorial of Enable Disable WiFi programmatically in android on button click. turn on and off wifi of your android device using the switch button. It is very simple to on wifi or off wifi from the android code.
please follow the code given below to Enable Disable WiFi programmatically in android on button click
- First Create a new project in Android Studio
- File ⇒ New Android ⇒ Application Project
- Then Open src -> package -> MainActivity.java and then add following code :
JAVA (MainActivity.java)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.wifi.WifiManager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import com.tutorial.personal.androidstudiopro.R; public class enableordisablewifiusingbutton extends AppCompatActivity { TextView WifiState; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_enableordisablewifiusingbutton); 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; } }}; } |
- Now Open res -> layout -> activity_main.xml and then add following code :
XML (activity_main.xml)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/wifistate" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintBottom_toTopOf="@+id/onwifi" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/onwifi" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Turn Wifi On" app:layout_constraintBottom_toTopOf="@+id/offwifi" app:layout_constraintTop_toBottomOf="@+id/wifistate" /> <Button android:id="@+id/offwifi" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="32dp" android:text="Turn Wifi Off" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toBottomOf="@+id/onwifi" /> </android.support.constraint.ConstraintLayout> |
Output:
Finally, run this project.
This is how application look like.