Your experience on this site will be improved by allowing cookies
To manage the wifi connectivity, such as to add network, disable the network, scan for access points, disconnect the network, etc., the android.net.wifi.WifiManager class can be used in Android.
In the below example, we are using the Android WifiManager to enable and disable the wifi service.
activity_main.xml:
Activity class:(File: MainActivity.java)
package com.example.radioapp; import android.net.wifi.WifiManager; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { Button enableButton,disableButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); enableButton=(Button)findViewById(R.id.button1); disableButton=(Button)findViewById(R.id.button2); enableButton.setOnClickListener(new OnClickListener(){ public void onClick(View v){ WifiManager wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE); wifi.setWifiEnabled(true); } }); disableButton.setOnClickListener(new OnClickListener(){ public void onClick(View v){ WifiManager wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE); wifi.setWifiEnabled(false); } }); } } |
AndroidManifest.xml:
In the AndroidManifest.xml file, we will write the code to add the below permissions.
Syntax:
0 comments