Android examples for Phone:Network
open/close 3G/2G data radio
import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import android.content.Context; import android.database.Cursor; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.Uri; import android.net.wifi.WifiManager; import android.telephony.TelephonyManager; import android.util.Log; public class Main{ /**/*from w w w . ja va2 s.c om*/ * open/close 3G/2G data radio * (not work on Ophone) * @param context * @param enabled * true-open; false-close * @throws ClassNotFoundException * @throws NoSuchFieldException * @throws SecurityException * @throws IllegalAccessException * @throws IllegalArgumentException * @throws NoSuchMethodException * @throws InvocationTargetException */ public static void toggleMobileData(Context context, boolean enabled) throws MobileDataException { try { ConnectivityManager connManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); Class<?> conMgrClass = Class.forName(connManager.getClass() .getName()); Field iConMgrField = conMgrClass.getDeclaredField("mService"); iConMgrField.setAccessible(true); Object iConMgr = iConMgrField.get(connManager); Class<?> iConMgrClass = Class.forName(iConMgr.getClass() .getName()); Method setMobileDataEnabledMethod = iConMgrClass .getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE); setMobileDataEnabledMethod.setAccessible(true); setMobileDataEnabledMethod.invoke(iConMgr, enabled); } catch (ClassNotFoundException e) { throw new MobileDataException(e); } catch (NoSuchFieldException e) { throw new MobileDataException(e); } catch (IllegalArgumentException e) { throw new MobileDataException(e); } catch (IllegalAccessException e) { throw new MobileDataException(e); } catch (NoSuchMethodException e) { throw new MobileDataException(e); } catch (InvocationTargetException e) { throw new MobileDataException(e); } } }