Tuesday 31 July 2012

Enable/disable data and WiFi connection in android programmatically


For accessing the WIFI state :

WifiManager wifiManager ;
wifiManager  = (WifiManager)this.getSystemService(this.WIFI_SERVICE);
wifiManager.setWifiEnabled(true);        //True - to enable WIFI connectivity .



For accessing the DATA/3G state :


ConnectivityManager dataManager;
dataManager  = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Method dataMtd = ConnectivityManager.class.getDeclaredMethod("setMobileDataEnabled", boolean.class);
dataMtd.setAccessible(true);
dataMtd.invoke(dataManager, true);        //True - to enable data connectivity .

Now you need to add this permissions in Manifest file :


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


9 comments:

  1. sir what kind of program to edit/make these?

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. did u put the uses-permission on manifest file ....

      Delete
  3. Method cannot be resolved to a type

    ReplyDelete
  4. Sir,
    I need an android wifi application that shows the scanned networks in a selected list view...so that after scanning when it shows available networks and when we select it ...it gets connected to any one...pls help me,,,am so stressed because of this...

    ReplyDelete
    Replies
    1. How to enable mobile internet data programatically?

      Delete
  5. Hi !! How can I do this in ruby ? Thank you !

    ReplyDelete
  6. Hi, marshmallow it is not working

    ReplyDelete
  7. How to turn off data connection programmatically

    ReplyDelete