Code to Enable dataconnection in Android 4.0.4 Tablet in Background -


i want enable 3g dataconnection through code, without user interaction. must done in background itself.

i tried codes found in google, nothing works in android tablet 4.0.4. inserted 3g sim-card, restarted device. code not automatically invoke dataconnection in tablet.my tablet internal memory name "sdcard2", reason there problem code. please give me appropriate solution this. used below code :

public static void enableinternet(context mycontext)         {             try {                 log.i("reached enable", "i 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             {               // used invoke hidden methods reflection                 class ctelman = null;                 method getitelephony = null;                 object otelephony = null;                 class ctelephony = null;                 method action = null;                  // current object implementing itelephony interface                 ctelman = m_telmanager.getclass();                 getitelephony = ctelman.getdeclaredmethod("getitelephony");                 getitelephony.setaccessible(true);                 otelephony = getitelephony.invoke(m_telmanager);                  // call 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 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"/>  

you need have instance of iconnectivitymanager class connectivitymanager object , use setmobiledataenabled() method on iconnectivitymanager instance.

try code below (tested on galaxy y (2.3) , 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"/>


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -