Monday, February 27, 2012

Android: Enable/Disable 3G programmatically


Here we go again,

Hint: this post will be just a wrap up to what you can find here.

Unfortunately, the data-connection enable/disable APIs are not exposed to the user, So we'll access them using JAVA reflection.

Starting from API level 8 (platform 2.2) we can use the following snippet:

 final ConnectivityManager conman =   
    (ConnectivityManager) context.getSystemService(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);   
      
 // (true) to enable 3G; (false) to disable it.   
 setMobileDataEnabledMethod.invoke(iConnectivityManager, true);   

We need to add the following permission to the manifest file

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

Hint: the previous piece of code was tested on SAMSUNG GALAXY SII API level 9(platform 2.3).

Ok, now I'll post another snippet that I got it working on the emulator for APIs levels prior to (8):

 Method dataConnSwitchmethod;  
 Class telephonyManagerClass;  
 Object ITelephonyStub;  
 Class ITelephonyClass;  
    
 TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);  
    
 if(telephonyManager.getDataState() == TelephonyManager.DATA_CONNECTED){  
    isEnabled = true;  
 }else{  
    isEnabled = false;  
 }  
    
 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 (isEnabled) {  
    // if you want to disable it.  
    dataConnSwitchmethod =  ITelephonyClass.getDeclaredMethod("disableDataConnectivity");  
 } else {  
    // if you want to enable it.  
    dataConnSwitchmethod = ITelephonyClass.getDeclaredMethod("enableDataConnectivity");  
 }  
    
 dataConnSwitchmethod.setAccessible(true);  
 dataConnSwitchmethod.invoke(ITelephonyStub);  

That's it, don't hesitate to comment, to share your knowledge and to correct me.

29 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Sorry. It does not run on Android 2.3/3.0

    ReplyDelete
  3. how to do this in a simple way?

    ReplyDelete
  4. Hi, can You write how i can use this on mobile phone with Dual Sim?

    ReplyDelete
  5. how can deactivate SIM card in android programming

    ReplyDelete
  6. <a href="https://alryan-services.com.sa/%d8%b4%d8%b1%d9%83%d8%a9-%d8%b9%d8%b2%d9%84-%d9%85%d9%88%d8%a7%d8%b3%d9%8a%d8%b1-%d8%a7%d9%8

    ReplyDelete
  7. Your solution for programmatically enabling/disabling 3G on Android is really useful! The reflection approach to access hidden APIs is clever, though I'm glad Android has made some of this functionality more accessible in later versions. Network management is such a critical part of mobile development, and it's only becoming more important as we deploy more network-dependent AI applications. Edge AI and mobile AI applications require efficient network usage, and understanding the underlying connectivity management is key. I'm currently exploring Gen AI and Agentic AI Learning course in Electronic City options to understand how mobile AI applications are being built. The course seems comprehensive and practical.

    ReplyDelete