Control 3G interface with your own application in Android

It is well known that we can use our application code to manipulate the WiFi interface (e.g, shutdown, bring up) with the WifiManager Service. For example:

   WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
   wifiManager.setWifiEnabled(True | False);

But manipulating the 3G interface is much harder.  There is no a explicit guide in Android’s developer documentation, and the only method I found is not supported through the standard SDK.

I looked up the source code of Android’s Settings application, in particular Menu –> Setting –> Wireless & networks –> Mobile networks –> Data enabled.

I found that it is implemented by calling ConnectivityManager.setMobileDataEnabled function.  But this function has two problems for normal applications:

  1. In the ConnectivityManager implementation, the setMobileDataEnabled is tagged hidden, which means that normal application can not trigger this function.
  2. The application needs the system privilege to execute the function. Otherwise, it throws a permission error.

I will demonstrate how to solve these problems to let your code manipulate the 3G interface.

Problem 1: we can use Java reflection method to look the function in the class instance. The code is as follows:

private void setMobileDataEnabled(boolean enable) {
ConnectivityManager cm =
    (ConnectivityManager) context
          .getSystemService(Activity.CONNECTIVITY_SERVICE);
Method m = getMethodFromClass(cm, "setMobileDataEnabled");
runMethodofClass(cm, m, enable);
}

private Method getMethodFromClass(Object obj, String methodName) {
    final String TAG = "getMethodFromClass";
    Class<?> whichClass = null;
    try {
        whichClass = Class.forName(obj.getClass().getName());
     } catch (ClassNotFoundException e2) {
         Log.d(TAG, "class not found");
     }
     Method method = null;
     try {
        //method = whichClass.getDeclaredMethod(methodName);
        Method[] methods = whichClass.getDeclaredMethods();
        for (Method m : methods) {
            if (m.getName().contains(methodName)) {
                method = m;
            }
        }
     } catch (SecurityException e2) {
       Log.d(TAG, "SecurityException for " + methodName);
      }
      return method;
}

private Object runMethodofClass(Object obj, Method method, Object... argv) {
    Object result = null;
    if (method == null) return result;
    method.setAccessible(true);
    try {
       result = method.invoke(obj, argv);
    } catch (IllegalArgumentException e) {
         Log.d(TAG, "IllegalArgumentException for " + method.getName());
    } catch (IllegalAccessException e) {
         Log.d(TAG, "IllegalAccessException for " + method.getName());
    } catch (InvocationTargetException e) {
          Log.d(TAG, "InvocationTargetException for " + method.getName()
            + "; Reason: " + e.getLocalizedMessage());
    }
   return result;
}

Problem2: Add “android:sharedUserId=”android.uid.system” into the <manifest > of the project’s AndroidManifest.xml file.  Then compile the application against the Android source code tree and compilation environment.

The following are steps for compiling my application (project name: InterfaceManager). My Android source code tree is in the /media/MyHardDisk/CyanogenMod directory.

# cd /media/MyHardDisk/CyanogenMod
# cp -r InterfaceManager packages/apps/
# build/environment.sh
# lunch; [and choose 32 (passion)]
# cd package/apps/InterfaceManage

Add an Andrild.mk file like:

 LOCAL_PATH := $(call my-dir)
 include $(CLEAR_VARS)
 LOCAL_SRC_FILES := $(call all-subdir-java-files)
 LOCAL_PACKAGE_NAME := InterfaceManager
 LOCAL_CERTIFICATE := platform
 include $(BUILD_PACKAGE)

Then:

# mm;

The “mm” command will compile the project. If it succeeds, it outputs the InterfaceManager.apk to the out/target/…/ directory. You can then use “adb install InterfaceManager.apk” to install the package manually.

Note:

  1. if your project created previously by Eclipse, Eclipse automatically generates gen/R.java file and you need to remove the gen directory when compiling with mm since Android compiler use a global R.java for all its applications.
  2. in your layout xml file, all the customized values, for example android:text = “OK”, must be defined in xml file in values directory such as android:text = “@string/ok”

Mar06

3 Responses to “Control 3G interface with your own application in Android”

  1. Hi,

    Thanks so much for a very useful post.

    I was able to follow all the steps except the last part where you compile the code against the android source tree. Can you expand a little more on that topic a little bit.

    I tried downloading the source tree and it gave me a errors. i was wondering if there’s any other way to utilize the internal APIs

    Any help will be much much appreciated.

    Thanks,
    Alin

  2. Max MEdina Says:
    May 18, 2012 at 5:26 am

    Hello,

    i can’t run the code.
    i keep getting this error:
    context cannot be resolved

    any help please.

  3. Good day veгу coοl web site!
    ! Guу .. Εxcellent .. Wonԁerful .. I ωill bοοkmark yоur blоg and taκе the feeds also?
    I’m glad to find so many useful information here within the post, we want work out extra strategies on this regard, thank you for sharing. . . . . .
    Everywhere accross the planet, Internet surfers get methods to continue to be confidential to your Websites they take a look at. Those self same buyers have already been trying to find methods to get access to well-known Web sites which can be plugged through whole teams of users. Individuals have trusted a great anonymizer or maybe Web proxy expert services to stay hidden or gain access to on the net Television, Xbox 360 system Stay, Hulu, and various main information sites from around the world.

    The most popular Russian language anonymizer provider HideME.ru has lastly fashioned a British Web-site for their providers named InCloak.net. “Our expert services permit consumers to modify its real IP address to your presented private Ip address that allows them to surf the net safely and securely devoid of abandoning a history in addition to obtain 100-percent entry to recently clogged Web sites,” reported an InCloak.world wide web expert.

    The actual InCloak.org anonymizer will allow you to line the host country involving origins, pick out virtually any obtainable Ip, filtration destructive website programs, reduce advertising banners and also increase popular and quite often seen Internet websites towards the program’s alexa tool. Even though the anonymizer is employed by a particular decided on program, a supplier is designed with a virtual Private Community (VPN) referred to as OpenVPN. Your VPN customer service offers greatest privacy as well as security for all software of which take advantage of the World wide web from your subscriber’s pc. “The major VPN advantage over every proxy or even anonymizer will be the approach it includes anonymity in addition to to safeguard the full pc instantly,” reported the particular specialized.

    InCloak.internet gives Site gear like World wide web proxy, proxies collection, Checker and more as little as $0.The spring per day or even VPN entry as little as $0.11 on a daily basis having one-year subscribers. Additional ideas contain day-to-day, regular plus two-year subscriptions. The actual high quality entry program delivers entire accessibility anonymizer (as well as VPN, should the code is usually purchased using this type of selection) without having limitations upon utilize. Quite a few additional features also are provided.

Leave a Reply