android - Toggle button for Mobile data -
in app having 2 toggle buttons, 1 wifi , other 1 mobile data. when applications start, if wifi on, toggle button on. but, if mobile data on, toggle button doesn't show that, it's still grey(no matter what's happening wifi). when press it, becomes green , mobile data still on... idea why?
gprs.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { try { turndata(ischecked); //klasa za ukljucivanje gprsa } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } } });
and class mobile data
void turndata(boolean on) throws exception { log.i("version:", "found gingerbread+"); final connectivitymanager conman = (connectivitymanager) getapplicationcontext().getsystemservice(context.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); setmobiledataenabledmethod.invoke(iconnectivitymanager, on); }
why using reflection this? if google, i'm not, put protections in api people can't use reflection f low level systems. allowing reflection kind of stuff make system vulnerable no 1 ever able use it.
if @ source tree see iconnectivitymanager isn't java class, it's aidl resource, meaning it's backed native code (c/c++) don't know reflection ever work there.
if @ setmobiledataenabled
method you're trying access, it's public in connectivitymanager source.
/** * sets persisted value enabling/disabling mobile data. * * @param enabled whether user wants mobile data connection used * or not. * @hide */ public void setmobiledataenabled(boolean enabled) { try { mservice.setmobiledataenabled(enabled); } catch (remoteexception e) { } }
i haven't used it, why try hack down underlying services , not use that?
Comments
Post a Comment