[Android ] Example code Enable/Disable Wifi or Internet Connection

Public on March 5, 2015
this is the important point of this problem.i found it on stackoverflow

WifiManager wifiManager = (WifiManager)this.context.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(status);
where status may be true or false as per requirement.

And this is my sample source code , get state of internet connection , enable or d
isable your connection immediatly.
NOTE : Note: To access with WiFi state, we have to add following permissions inside the AndroidManifest.xml file:
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />


This is how you get your connection state :
---------------------------

public boolean isConnectingToInternet(){
            ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
              if (connectivity != null) 
              {
                  NetworkInfo[] info = connectivity.getAllNetworkInfo();
                  if (info != null) 
                      for (int i = 0; i < info.length; i++) 
                      { 
                           String temp =  "Type name : "+info[i].getTypeName()+"\nType : "+info[i].getType()
                          +"\nReason : "+info[i].getReason();  
                           arrString.add(temp);//get infor
                         
                          if (info[i].getState() == NetworkInfo.State.CONNECTED)
                          {
                              return true;
                          }
                      }
     
              }
              return false;
        }



----------------------------

and this is how to enable or disable your internet connection.

WifiManager wifiManager = (WifiManager)this.context.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(status);

Result :  Download Source Code Here


 






[Advertising]Faster Thinking - Speed up your brain


Faster Thinking Game



sentiment_satisfied Emoticon