Note that there are some explanatory texts on larger screens.

plurals
  1. POCode to Enable dataconnection in Android 4.0.4 Tablet in Background
    primarykey
    data
    text
    <p>I want to enable my 3g DataConnection through code, without any user interaction. Everything must be done in background itself.</p> <p>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 :</p> <pre><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(); } </code></pre> <hr> <p>I used below permissions also i Manifest file :</p> <pre><code>&lt;uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /&gt; &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt; &lt;uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/&gt; &lt;uses-permission android:name="android.permission.INTERNET" /&gt; &lt;uses-permission android:name="android.permission.ACCESS_WIFI_STATE"&gt;&lt;/uses-permission&gt; &lt;uses-permission android:name="android.permission.UPDATE_DEVICE_STATS"&gt;&lt;/uses-permission&gt; &lt;uses-permission android:name="android.permission.INTERNET" /&gt; &lt;uses-permission android:name="android.permission.MODIFY_PHONE_STATE" /&gt; &lt;uses-permission android:name="android.permission.USE_CREDENTIALS" /&gt; &lt;uses-permission android:name="android.permission.MANAGE_ACCOUNTS" /&gt; &lt;uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" /&gt; &lt;uses-permission android:name="android.permission.READ_SYNC_STATS" /&gt; &lt;uses-permission android:name="android.permission.READ_SYNC_SETTINGS" /&gt; &lt;uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload