WiFi state is not enabling

前端 未结 2 478
旧时难觅i
旧时难觅i 2021-01-12 07:41

I am trying to create a widget for enabling and disabling the wifi.

if(myWifiManager.isWifiEnabled()){
            System.out.println(\"Toggle Wifi Enabled          


        
2条回答
  •  忘掉有多难
    2021-01-12 07:48

    Here is how to turn on and turn off wifi in android.

    First you need to declare the following in your manifest file

    
    
    
    
    

    After doing it that on your Activity class

    private WifiManager wifiManager;
    @Override 
    public void onCreate(Bundle icicle) {
     ....................
     wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
     if(wifiManager.isWifiEnabled()){
     wifiManager.setWifiEnabled(false);
     }else{
    wifiManager.setWifiEnabled(true);
    }
    

    }

    Explanation

    Get the Wifi service from our system

    wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
    

    Check the our wifi is currently turned on or turned off

    if(wifiManager.isWifiEnabled()){
    

    Turn on/off our wifi wifiManager.setWifiEnabled();

    Reference

    WifiEnabler

    http://google-androidlovers.blogspot.com/2012/01/scan-for-wireless-networks-in-android.html

    http://www.java2s.com/Open-Source/Android/android-platform-apps/Settings/com/android/settings/wifi/WifiApEnabler.java.htm

提交回复
热议问题