How to provide option to select wi-fi or GPRS for network connectivity in android app
Asked Answered
G

3

9

In my app i want to provide the user with the option to choose wi-fi / GPRS for network connectivity to the web server. May be answers to the following ques solve my prob ... 1. How to check which is the current default network connectivity option enabled. 2. How to enable wi-fi/GPRS on user selection or (disable the wi-fi if user chooses GPRS - if only this option will be required for GPRS to work)

or is there some other way around to do this ?

Greasepaint answered 26/7, 2012 at 5:33 Comment(0)
L
15

Try this:

ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

if (mWifi.isConnected())
    //if wifi connected
}

ConnectivityManager connManager1 = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo mMobile = connManager1.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

if (mMobile.isConnected()) {
    //if internet connected
}

Don't forget to add these permissions in the manifest file;

android.permission.CHANGE_WIFI_STATE
android.permission.ACCESS_WIFI_STATE
android.permission.UPDATE_DEVICE_STATS
android.permission.CHANGE_NETWORK_STATE
android.permission.ACCESS_NETWORK_STATE
android.permission.MODIFY_PHONE_STATE
android.permission.READ_PHONE_STATE

To enable or disable Wifi, use mWiFi.setWifiEnabled(true|false)

To enable/disable GPRS/3G, use the following code snippet.

void turnData(boolean ON) throws Exception
{

if(bv == Build.VERSION_CODES.FROYO)
{

    Log.i("version:", "Found Froyo");
    try{ 
        Method dataConnSwitchmethod;
        Class telephonyManagerClass;
        Object ITelephonyStub;
        Class ITelephonyClass;
        TelephonyManager telephonyManager = (TelephonyManager) getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);

        telephonyManagerClass = Class.forName(telephonyManager.getClass().getName());
    Method getITelephonyMethod = telephonyManagerClass.getDeclaredMethod("getITelephony");
    getITelephonyMethod.setAccessible(true);
    ITelephonyStub = getITelephonyMethod.invoke(telephonyManager);
    ITelephonyClass = Class.forName(ITelephonyStub.getClass().getName());

    if (ON) {
         dataConnSwitchmethod = ITelephonyClass.getDeclaredMethod("enableDataConnectivity"); 

    } else {
        dataConnSwitchmethod = ITelephonyClass.getDeclaredMethod("disableDataConnectivity");
    }
    dataConnSwitchmethod.setAccessible(true);
    dataConnSwitchmethod.invoke(ITelephonyStub);
    }catch(Exception e){
          Log.e("Error:",e.toString());
    }

}
 else
{
   Log.i("version:", "Found Gingerbread+");
   final ConnectivityManager conman = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
   final Class conmanClass = Class.forName(conman.getClass().getName());
   final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
   iConnectivityManagerField.setAccessible(true);
   final Object iConnectivityManager = iConnectivityManagerField.get(conman);
   final Class iConnectivityManagerClass =  Class.forName(iConnectivityManager.getClass().getName());
   final Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
   setMobileDataEnabledMethod.setAccessible(true);
   setMobileDataEnabledMethod.invoke(iConnectivityManager, ON);
}

}

Larrisa answered 26/7, 2012 at 5:50 Comment(1)
+1 for the internet data though I didn't manage to activate wifi this way because setWifiEnabled is a method of WifiManager This worked for me WifiManager wifiManager = (WifiManager)this.getApplicationContext().getSystemService(Context.WIFI_SERVICE); wifiManager.setWifiEnabled(true);Torpor
S
3

You can provide option to user on screen using following code block....

 public static ShowAvailable() 
 {
   ConnectivityManager connectivityMgr = (ConnectivityManager)
   getSystemService(Context.CONNECTIVITY_SERVICE);
   NetworkInfo[] nwInfos = connectivityMgr.getAllNetworkInfo(); 
       for (NetworkInfo nwInfo : nwInfos)  
          {  
             Log.d(TAG, "Network Type Name: " +
             nwInfo.getTypeName());   Log.d(TAG, "Network available: " +
             nwInfo.isAvailable());   Log.d(TAG, "Network c_or-c: " +
             nwInfo.isConnectedOrConnecting());   Log.d(TAG, "Network connected: "
             + nwInfo.isConnected()); 
          }

}
Steiermark answered 26/7, 2012 at 5:50 Comment(0)
S
1

Continuing the @rIHaN JiTHiN answer, it should be noted that two permissions android.permission.UPDATE_DEVICE_STATS and android.permission.MODIFY_PHONE_STATE are only granted to system app (as it is initially set in Eclipse or Android Studio). So if someone will be faced with this issue, look at this solution.

Sweetbread answered 30/4, 2014 at 22:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.