Android, How to handle change in network (from GPRS to Wi-fi and vice-versa) while polling for data

You can set up a Receiver in your manifest: <receiver android:name=”.NetworkChangeReceiver” android:label=”NetworkChangeReceiver”> <intent-filter> <action android:name=”android.net.conn.CONNECTIVITY_CHANGE” /> <action android:name=”android.net.wifi.WIFI_STATE_CHANGED” /> </intent-filter> </receiver> And implement the Receiver with something like this: public class NetworkChangeReceiver extends BroadcastReceiver { @Override public void onReceive(final Context context, final Intent intent) { final ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); final android.net.NetworkInfo wifi = … Read more

Create Network Access Point Name with code,

I will give some examples: Getting default APN information: //path to APN table final Uri APN_TABLE_URI = Uri.parse(“content://telephony/carriers”); //path to preffered APNs final Uri PREFERRED_APN_URI = Uri.parse(“content://telephony/carriers/preferapn”); //receiving cursor to preffered APN table Cursor c = getContentResolver().query(PREFERRED_APN_URI, null, null, null, null); //moving the cursor to beggining of the table c.moveToFirst(); //now the cursor points to … Read more

Enable/disable data connection in android programmatically

This code sample should work for android phones running gingerbread and higher: private void setMobileDataEnabled(Context context, boolean enabled) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException { final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); final Class conmanClass = Class.forName(conman.getClass().getName()); final Field connectivityManagerField = conmanClass.getDeclaredField(“mService”); connectivityManagerField.setAccessible(true); final Object connectivityManager = connectivityManagerField.get(conman); final Class connectivityManagerClass = Class.forName(connectivityManager.getClass().getName()); final Method setMobileDataEnabledMethod … Read more