How to enable/disable WiFi from an application?
You also need to request the permission in your AndroidManifest.xml :
@Codii, I know this is old, but I am trying to do this within a dialogfragment. However, it says «Cannot resolve method ‘getSystemService(java.lang.String)’ I am not sure on what I need to do. And I have those permissions
To enable/disable WiFi in your application you need to use WiFiManager class. Create an Object of WiFiManager class to get the services of WiFi.
WifiManager wifi; wifi=(WifiManager)getSystemService(Context.WIFI_SERVICE); wifi.setWifiEnabled(false);//Turn off Wifi wifi.setWifiEnabled(true);//Turn on Wifi
And you have to put the following permissions in AndroidManifest.xml
To get the whole sample code of enable/disable Wifi in android with UI visit this website
when i do it in my project, but when these code is executed, the system will give a prompt says the app is trying to use wlan whether allows it. and it appears every time! can i enable wlan without the prompt window .
Intent gpsOptionsIntent = new Intent( android.provider.Settings.ACTION_WIFI_SETTINGS); startActivityForResult(gpsOptionsIntent,0);
To enable/disable wifi from an app in Android Q (Android 10) use Settings Panel:
val panelIntent = Intent(Settings.Panel.ACTION_INTERNET_CONNECTIVITY) startActivityForResult(panelIntent, 0)
On previous versions of Android this should work (appropriate permissions should be added to AndroidManifest file, see answers above):
(context?.getSystemService(Context.WIFI_SERVICE) as? WifiManager)?.apply < isWifiEnabled = true /*or false*/ >
Resulting code might look something like this:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) < val panelIntent = Intent(Settings.Panel.ACTION_INTERNET_CONNECTIVITY) startActivityForResult(panelIntent, 0) >else < (context?.getSystemService(Context.WIFI_SERVICE) as? WifiManager)?.apply < isWifiEnabled = true /*or false*/ >>
Where context is a reference to android.content.Context object.
WIFI_STATE_CHANGED_ACTION intent not received upon connection to WiFi access point?
Part of my app’s functionality is to scan and display a list of WiFi access points, and then connect to the one chosen by the user. I have this functionality working. Now, I also wish to be notified when the connection «goes through». This should be fairly simple, but I find myself stumbling. I have read through various posts here at SO, and they all mention registering for WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION or WifiManager.WIFI_STATE_CHANGED_ACTION . However, neither of these works for me. Can anyone spot any mistake in this code? (I am leaving out the parts which do the scan and stuff) Expected behavior: As soon as the connection is successful (i.e, when I see the «connected» icon on the notification bar), the broadcast should be received and I should see the toast. Observed behavior: The broadcast is received when the app first starts, and whenever I switch back to it (i.e, whenever onResume() is called; or I suspect, whenever I register for the intent)
public class WifiScanActivity extends Activity < WifiManager mainWifi; WifiReceiver mWifiReceiver; IntentFilter mIntentFilter; private final static String TAG = "WifiScanActivity"; public static final String INTENT_FOR_WIFI_CONNECTED = WifiManager.WIFI_STATE_CHANGED_ACTION; // public static final String INTENT_FOR_WIFI_CONNECTED = // WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION; public void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.main); mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); mWifiReceiver = new WifiReceiver(); mIntentFilter = new IntentFilter(); mIntentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION); mIntentFilter.addAction(INTENT_FOR_WIFI_CONNECTED); registerReceiver(mWifiReceiver, mIntentFilter); mainWifi.startScan(); >protected void onPause() < unregisterReceiver(mWifiReceiver); super.onPause(); >protected void onResume() < registerReceiver(mWifiReceiver, mIntentFilter); super.onResume(); >class WifiReceiver extends BroadcastReceiver < public void onReceive(Context c, Intent intent) < Log.d(TAG, "In WifiReceiver: Broadcast Received = " + intent.getAction()); if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(intent .getAction())) < // Display the ListView and connect to the selected AP >else if (INTENT_FOR_WIFI_CONNECTED.equals(intent.getAction())) < if (WifiManager.WIFI_STATE_ENABLED == intent.getIntExtra( WifiManager.EXTRA_WIFI_STATE, 0)) < displayNetworkInfo(); >/*if(true == intent.getBooleanExtra( * WifiManager.EXTRA_SUPPLICANT_CONNECTED, false))< * displayNetworkInfo(); >*/ > > > private void displayNetworkInfo() < WifiInfo wifiInfo = mainWifi.getConnectionInfo(); String ssid = wifiInfo.getSSID(); int ip = wifiInfo.getIpAddress(); String message = "Connection established.\nSSID = " + ssid + "; IP Address mt24 mb12">androidandroid-wifi