0

I want to enable my 3g DataConnection through code, without any user interaction. Everything must be done in background itself.

I tried all the codes I found in Google, but nothing works in my Android Tablet 4.0.4. I inserted the 3g sim-card, restarted the device. But the code will not automatically invoke the DataConnection in Tablet.My Tablet internal memory name will be "SDCard2", for this reason is there any problem with that code. Please give me Appropriate solution for this. I used below code :

public static void EnableInternet(Context mycontext)
        {
            try {
                Log.i("Reached Enable", "I am here");
                Process proc;
                try {
                    proc = Runtime.getRuntime().exec( "su" );
                    try {
                        proc.waitFor();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }          
                setMobileDataEnabled(mycontext,true);
            } catch (NoSuchFieldException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    void turnData(boolean ON)
         {
ConnectivityManager iMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
            Method iMthd = null;
            try {
                iMthd = ConnectivityManager.class.getDeclaredMethod("setMobileDataEnabled", boolean.class);
                } catch (Exception e) {
                       } 
            iMthd.setAccessible(false);

            if(ON)
             {

                        try {
                            iMthd.invoke(iMgr, true);
                            Toast.makeText(getApplicationContext(), "Data connection Enabled", Toast.LENGTH_SHORT).show();
                        } catch (IllegalArgumentException e) {
                            // TODO Auto-generated catch block
                            // dataButton.setChecked(false);
                             Toast.makeText(getApplicationContext(), "IllegalArgumentException", Toast.LENGTH_SHORT).show();

                        } catch (IllegalAccessException e) {
                            // TODO Auto-generated catch block
                            Toast.makeText(getApplicationContext(), "IllegalAccessException", Toast.LENGTH_SHORT).show();

                            e.printStackTrace();
                        } catch (InvocationTargetException e) {
                            // TODO Auto-generated catch block
                            // dataButton.setChecked(false);
                             Toast.makeText(getApplicationContext(), "InvocationTargetException", Toast.LENGTH_SHORT).show();

                        }

             }
            else
             {
                try {
                    iMthd.invoke(iMgr, true);
                    Toast.makeText(getApplicationContext(), "Data connection Disabled", Toast.LENGTH_SHORT).show();
                    } catch (Exception e) {
                          // dataButton.setChecked(true);
                        Toast.makeText(getApplicationContext(), "Error Disabling Data connection", Toast.LENGTH_SHORT).show();
                                            }
             }}





    boolean switchState(boolean enable) 
    {
        boolean bRes = false;

        // Data Connection mode (only if correctly initialized)
        if (m_telManager != null)
        {
            try
            {
              // Will be used to invoke hidden methods with reflection
                Class cTelMan = null;
                Method getITelephony = null;
                Object oTelephony = null;
                Class cTelephony = null;
                Method action = null;

                // Get the current object implementing ITelephony interface
                cTelMan = m_telManager.getClass();
                getITelephony = cTelMan.getDeclaredMethod("getITelephony");
                getITelephony.setAccessible(true);
                oTelephony = getITelephony.invoke(m_telManager);

                // Call the enableDataConnectivity/disableDataConnectivity method
                // of Telephony object
                cTelephony = oTelephony.getClass();
                if (enable)
                {
                    action = cTelephony.getMethod("enableDataConnectivity");
                }
                else
                {
                    action = cTelephony.getMethod("disableDataConnectivity");
                }
                action.setAccessible(true);
                bRes = (Boolean)action.invoke(oTelephony);
            }
            catch (Exception e)
            {
                bRes = false;
            }
        }        
        return bRes;
    }     





    try {
   ConnectivityManager mgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
                Method dataMtd = ConnectivityManager.class.getDeclaredMethod("setMobileDataEnabled", boolean.class);
                dataMtd.setAccessible(true);
                dataMtd.invoke(mgr, true);
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

I used below permissions also i Manifest file :

<uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.INTERNET" />

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

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" /> 
<uses-permission android:name="android.permission.USE_CREDENTIALS" /> 
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" /> 
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" /> 
<uses-permission android:name="android.permission.READ_SYNC_STATS" /> 
<uses-permission android:name="android.permission.READ_SYNC_SETTINGS" /> 
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/> 
plsgogame
  • 1,334
  • 15
  • 28
Pavan Tilak
  • 153
  • 1
  • 1
  • 7

1 Answers1

0

You need to have the instance of IConnectivityManager class from the connectivityManager object and then use the setMobileDataEnabled() method on the iConnectivityManager instance.

Try the code below (tested on Galaxy Y (2.3) and Nexus 4 (4.2)) :

public void cellularState(boolean shouldEnable) {
    ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    try {
        Class<?> conmanClass = Class.forName(conman.getClass().getName());
        Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
        iConnectivityManagerField.setAccessible(true);
        Object iConnectivityManager = iConnectivityManagerField.get(conman);

        Class<?> iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());
        Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
        setMobileDataEnabledMethod.setAccessible(true);

        setMobileDataEnabledMethod.invoke(iConnectivityManager, shouldEnable);
    } catch(IllegalAccessException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 

In manifest declare:

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

Srikant Sahay
  • 885
  • 6
  • 9
  • I tried this one in my Android 4.0.4 tablet , but it doesnot work – Pavan Tilak Aug 01 '13 at 09:47
  • Does it throw any errors? And BTW you don't neet "su" for this. Please check logcat to see if there are errors thrown for your app – Srikant Sahay Aug 01 '13 at 10:03
  • Hi ,iam not getting any error and also i didnot used that "su" in my code..because my device is not rooted.. – Pavan Tilak Aug 01 '13 at 10:43
  • Can you try it on another device. I haven't tried it on a tablet but on the phone it works. If it works on a phone for you but not on the tablet, I'm out of ideas. All other techniques that I tried would not work for some android os version or the other. This I found to be most reliable. Try to make a test activity with just this method and call it in onCreate to make sure nothing else is mucking with the code. Also make sure that wifi is turned off otherwise wifi connection will take precedence over your 3g connection. – Srikant Sahay Aug 01 '13 at 11:04
  • i tried that one yar ,but no use..but i tried my code in all tablets and no use – Pavan Tilak Aug 01 '13 at 11:41
  • There are a couple of techniques here : http://stackoverflow.com/questions/3644144/how-to-disable-mobile-data-on-android . You can try them out. Beyond that I'm out of ideas :( – Srikant Sahay Aug 01 '13 at 11:47
  • Previously i also tried that one yar..but no use..any way thank you very much for spending your valuable time – Pavan Tilak Aug 01 '13 at 12:26
  • Hi upto yet i didn't get any solution for my problem...is there any one please help me.. – Pavan Tilak Aug 02 '13 at 09:29