How to provide the option to choose Wi-Fi or GPRS to connect to the network in the Android application

In my application, I want to give the user the opportunity to choose Wi-Fi / GPRS for a network connection to a web server. There may be answers to the following questions to solve my problem ... 1. How to check if the network connection option is enabled by default. 2. How to enable wi-fi / GPRS when the user selects or (disable wi-fi if the user selects GPRS - if only this option is required for GPRS to work)

or is there any other way to do this?

+9
android android wifi gprs
Jul 26 2018-12-12T00:
source share
3 answers

Try the following:

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 } 

Remember to add these permissions to 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 Wi-Fi, 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); } 

}

+15
Jul 26 '12 at 5:50
source share

You can provide the user with an opportunity on the screen using the 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()); } } 
+3
Jul 26 '12 at 5:50
source share

Continuing with @rIHaN JiTHiN's answer, it should be noted that the two permissions android.permission.UPDATE_DEVICE_STATS and android.permission.MODIFY_PHONE_STATE granted only to the system application (since it was originally installed in Eclipse or Android Studio). Therefore, if someone encounters this problem, see this solution .

+1
Apr 30 '14 at 22:20
source share



All Articles