How to turn 3G on and off in Android -


this question has answer here:

i android developer, want run 3g connection on or off via button in application.

i've done extensive search on topic , must tell there no simple solution problem on modern android versions, unless run app root.

i found simple, yet bit limited, workaround though: 3g connectivity active if wifi down, each time initialize network connection, must check if wifi , connected. below code sample activity:

public class myactivity extends activity {     boolean enable3g = false;      public void oncreate() {         setcontentview(r.layout.my_layout);         button enable3gbutton = (button) findviewbyid(r.id.button3g);         enable3gbutton.setonclicklistener(new onclicklistener() {             @override             public void onclick(view v) {                 myactivity.this.enable3g = !myactivity.this.enable3g;             }         });          if (enable3g || iswificonnected()) {             // stuff requires network access here         }        }     public boolean iswificonnected() {         connectivitymanager connectivitymanager = (connectivitymanager) getsystemservice(context.connectivity_service);         return connectivitymanager.getnetworkinfo(connectivitymanager.type_wifi).isconnected();     } } 

in androidmanifest need access_network_state permission (and declaration of activity of course):

<uses-permission android:name="android.permission.access_network_state" />  <application .....>     <activity android:name="myactivity" /> 

if wifi not connected (the iswificonnected() method returns false), not start connect. existing tcp connection cannot "jump" 1 network time out if wifi connectivity broken, making solution quite safe.

the limitation is, must able control application's connections. if using webview, or 3rd-party piece of software, may tricky.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -