Android - How to enable and disable wifi

Asked

Viewed 1,102 times

0

I have an app in java with two buttons. How to do when press the first wifi is activated, and when press the second it be disabled?

Thank you.

1 answer

3


You can use the class WifiManager and through the method setWifiEnabled can enable and disable wifi, official documentation can be found here.

To do this you must add the permission in the manifest of your application.

On your Androidmanifest.xml add the following permission:

<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

and in its programme to implement:

   WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
   wifi.setWifiEnabled(false); //desabilita
   wifi.setWifiEnabled(true); //habilita
  • I was convinced that this was no longer possible in the latest versions of Android.

  • Can you help me? the code worked but when I tap on it works perfectly, but displays the message the app "stopped" as I fix?

  • and when do to turn off the wifi displays the same message but does not turn off

  • @ramaral works with bluetooth also, only that does not work this way, is the GPS, do not know why..

  • @Drkill32 What error occurs? you can post here?

  • When I touch the button connect the wifi and activated. However the app closes alone...

  • But you can post the adb log to see the error??

  • Sorry adb? As far as I know there is no log. I am testing directly by smartphone

  • Are you using which IDE to program? If it is Intelijj or Android Studio just debug the app, will appear the console with the application events.

  • I am programming by own android because my PC is not working. I am using AIDE

  • 1

    @Marcogiovanni With GPS already worked but was removed for security and confidentiality reasons: any program could turn on the GPS and monitor the user’s location without him knowing.

  • @Drkill32 I do not know how to see the logs in AIDE, I used it only when it was released to make a 'hello word', without the logs there is no way to know what is happening.. :/

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.